Reputation: 577
I have a server that creates an object blob on the browser and I want to download this within WebView in an Android app. I tried redirecting the request to the browser instance as well as using the download manager to do this, but neither of them seems to work (Even though if I open up the same page in Chrome, the download operation works there).
I tried the following code,it throws error:
Android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=blob:https%3A//111.111.111.111%3A8080/40b63131-63b1-4fa4-9451-c6297bbd111a"
Edit
Android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=blob:http://digitalinsensu.com/0f0d6127-a0f1-44c3-af85-72f648258d6d
Code:
mWebView.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);
}
});
and this throws java.lang.IllegalArgumentException: Can only download HTTP/HTTPS URIs error:
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,String contentDisposition, String mimetype,long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});
How should I be downloading the blob? Any help would be appreciated.
Upvotes: 10
Views: 20802
Reputation: 47965
You need to intercept the callback for handling url clicks like this:
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
shouldOverrideUrlLoading(view, Uri.parse(url));
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return shouldOverrideUrlLoading(view, request.getUrl());
}
// legacy callback
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return shouldOverrideUrlLoading(view, Uri.parse(url));
}
return super.shouldOverrideUrlLoading(view, url);
}
private boolean shouldOverrideUrlLoading(final WebView view, final Uri request) {
if(request.getScheme().equals("blob")) {
// do your special handling for blob urls here
return true;
}
}
});
In shouldOverrideUrlLoading()
you can do your special handling.
Upvotes: 0