gpt4 book ai didi

android - 我的代码不会下载 Excel 或 CSV 文件,其他文件类型可以正常工作

转载 作者:行者123 更新时间:2023-11-29 23:29:35 25 4
gpt4 key购买 nike

我正在尝试通过 webview 在 android 中下载 excel 和 csv 文件。

正在下载其他文件类型。

每次我尝试下载文件(excel、csv)时,应用程序都会停止响应。

这是我的代码

 public static void download(Activity mActivity, String url, String contentDisposition, String mimetype) {
if (ActivityCompat.checkSelfPermission(mActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
return;
}

To notify the Client that the file is being downloaded

            Toast.makeText(mActivity, R.string.downloading, Toast.LENGTH_LONG).show();
final String filename = URLUtil.guessFileName(url, contentDisposition, mimetype);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

request.setVisibleInDownloadsUi(true);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
DownloadManager dm = (DownloadManager) mActivity.getSystemService(Activity.DOWNLOAD_SERVICE);
dm.enqueue(request);
}

Thank you in Advance

最佳答案

只需将其放入您的代码中并通过 Webview 设置您正在下载的扩展

mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
Request request = new Request(Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
String newString ="THE FILE NAME.mp3"
Log.i(ERROR, "this is it " + newString);
File sdCard = Environment.getExternalStorageDirectory();
String folder = sdCard.getAbsolutePath();
request.setDestinationInExternalPublicDir(folder, newString);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
}
}

希望对您有所帮助。

关于android - 我的代码不会下载 Excel 或 CSV 文件,其他文件类型可以正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52864404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com