如何将用JS创建的csv文件下载到您的安卓手机(网络视图)



我想导出一个用JS创建的csv文件,让人们在手机上下载。

这是我用于创建文件的JS代码:

var createACSVFile = function () {
var ArrayOfDataToExport = [];
for (var k = 0; k < localStorage.length; k++) {
console.log([localStorage.key(k),JSON.parse(localStorage.getItem(localStorage.key(k)))]);
ArrayOfDataToExport.push([localStorage.key(k),JSON.parse(localStorage.getItem(localStorage.key(k)))])
}

var csvRows = [];
for(var i=0, l=ArrayOfDataToExport.length; i<l; ++i){
csvRows.push(ArrayOfDataToExport[i].join(','));
}
var csvString = csvRows.join("%0A");
var a         = document.createElement('a');
a.href        = 'data:attachment/csv,' + csvString;
a.target      = '_blank';
a.download    = 'exportFile.csv';
document.body.appendChild(a);
a.click();
};
createACSVFile();

这是我的安卓代码:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setAllowFileAccess(true);
view.getSettings().setDomStorageEnabled(true);
view.getSettings().setUseWideViewPort(true);
view.getSettings().setLoadWithOverviewMode(true);
view.setInitialScale(1);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setSupportZoom(false);
view.setWebViewClient(new MyBrowser(){
@Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.webView).setVisibility(View.VISIBLE);
}

});
view.loadUrl("file:///android_asset/www/index.html");
view.setWebChromeClient(new WebChromeClient(){
});
view.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}

我收到此错误:

08-20 11:12:36.508 17111-17111/checker.coin.crypto.wingcrony.by.cryptocoinchecker E/Android运行时:致命异常:main 进程: checker.coin.crypto.wingcrony.by.cryptocoinchecker, PID: 17111 android.content.ActivityNotFoundException:未找到要处理的活动 Intent { act=android.intent.action.VIEW dat=data:attachment/csv,Poloniex, 货币显示,美元 如何订购,简称 通过,假 哪个交易所你使用,Bitfinex } 在 android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1809( 在 android.app.Instrumentation.execStartActivity(Instrumentation.java:1523( at android.app.Activity.startActivityForResult(Activity.java:3981( at android.app.Activity.startActivityForResult(Activity.java:3933( at android.app.Activity.startActivity(Activity.java:4272( at android.app.Activity.startActivity(Activity.java:4240( 在 checker.coin.crypto.wingcrony.by.cryptocoinchecker.MainActivity$3.onDownloadStart(MainActivity.java:153( 在 com.android.webview.chromium.WebViewContentClientAdapter.onDownloadStart(WebViewContentClientAdapter.java:1195( 在 org.chromium.android_webview。AwContentClientCallbackHelper$MyHandler.handleMessage(AwContentClientCallbackHelper.java:126( at android.os.Handler.dispatchMessage(Handler.java:111( at android.os.Looper.loop(Looper.java:207( at android.app.ActivityThread.main(ActivityThread.java:5728( at java.lang.reflect.Method.invoke(Native Method( 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789( at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679(

更新

如果将其添加到我的安卓清单中.xml我会收到相同的错误:

<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>

我所做的是生成我需要在 JS 中导出到 csv 的数据,如下所示:

var generateTheDataToExportToArrayString = function () {
var ArrayOfDataToExport = [];
for (var k = 0; k < localStorage.length; k++) {
var keyName = (localStorage.key(k));
var data = JSON.parse(localStorage.getItem(localStorage.key(k)));
}
var String = '[';
for (var i = 0; i < ArrayOfDataToExport.length; i++){
if (typeof ArrayOfDataToExport[i][1] === "object"){
String += "[" + ArrayOfDataToExport[i][0] + ",";
if (ArrayOfDataToExport[i][1].length > 1 || ArrayOfDataToExport[i][1].length === 0){
if (ArrayOfDataToExport[i][1].length === 0){
String += '[' + '' + ']]';
}
else {
for (var k = 0; k < ArrayOfDataToExport[i][1].length; k++){
String += '[';
for (var l = 0; l < ArrayOfDataToExport[i][1][k].length - 1; l++){
String += ArrayOfDataToExport[i][1][k][l]+ ",";
}
String += ArrayOfDataToExport[i][1][k][ArrayOfDataToExport[i][1][k].length - 1] + "],";
}
String = String.slice(0, -1);
String += "],";
}
}
else {
String += "[";
for (var j = 0; j < ArrayOfDataToExport[i][1].length - 2; j++){
String += ArrayOfDataToExport[i][1][j] + ",";
}
String += ArrayOfDataToExport[i][1][ArrayOfDataToExport[i][1].length - 1] + "]],";
}
}
else {
String += "[" + ArrayOfDataToExport[i][0] + ",";
String += ArrayOfDataToExport[i][1] + "],";
}
}
String = String.slice(0, -1);
String += "]";
console.log(String);
};

你说我做的最后一件事就是记录那个字符串。好吧,我将在 Java 代码中捕获如下:

public void onConsoleMessage(String message, int lineNumber, String sourceID) {
if (sourceID.equals("file:///android_asset/www/assets/js/parseTest.js") && lineNumber == 184 ){
generateNoteOnSD(getBaseContext(),"export.csv", message);
}
}

这是函数生成注意在SD:

public void generateNoteOnSD(Context context, String sFileName, String sBody) {
try {
File root = new File(Environment.getExternalStorageDirectory(), "Backup_Data_For_AltFolio");
if (!root.exists()) {
root.mkdirs();
}
File gpxfile = new File(root, sFileName);
FileWriter writer = new FileWriter(gpxfile);
writer.append(sBody);
writer.flush();
writer.close();
Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}

相关内容

  • 没有找到相关文章

最新更新