我正在尝试通过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;
}
通知客户端文件正在下载
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);
}
提前谢谢
答案 0 :(得分:1)
只需将其放入代码中,然后设置要通过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);
}
}
希望这可以为您提供帮助。