我的代码不会下载 Excel 或 CSV 文件,其他文件类型可以正常工作
My code won't download Excel or CSV files, other file types work OK
我正在尝试通过网络视图下载 excel 和 android 中的 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);
}
}
希望对您有所帮助。
我正在尝试通过网络视图下载 excel 和 android 中的 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);
}
}
希望对您有所帮助。