Issue
I am trying to download excel and csv file in android through webview.
Other File types are downloading.
Everytime i try to download the file(excel,csv) the app stop responding.
Here is My Code
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
Solution
Just put this in your code and set the extension you are downloading through the 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);
}
}
Hope This could Help you.
Answered By - Syed Ali Shahzil
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.