Reputation: 181
I want to download specific file inside android webview app ( although they are from external link not from my webview app URL) and all external link should open outside webview app means phone browser.
I used code below to do that.
wv.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean value = true;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
String mimeType = mime.getMimeTypeFromExtension(extension);
if (mimeType != null) {
if (mimeType.toLowerCase().contains("pdf")
|| extension.toLowerCase().contains("ppt")
|| extension.toLowerCase().contains("doc")
|| extension.toLowerCase().contains("rar")
|| extension.toLowerCase().contains("rtf")
|| extension.toLowerCase().contains("exe")
|| extension.toLowerCase().contains("apk")
|| extension.toLowerCase().contains("jpeg")
|| extension.toLowerCase().contains("png")
|| extension.toLowerCase().contains("xls")
|| extension.toLowerCase().contains("zip")
|| extension.toLowerCase().contains("jpg")) {
DownloadManager mdDownloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
String name= URLUtil.guessFileName(url,null,MimeTypeMap.getFileExtensionFromUrl(url));
File destinationFile = new File(Environment.getExternalStorageDirectory(),name);
request.setDescription("Downloading...");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
// request.setDestinationUri(Uri.fromFile(destinationFile));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,name);
mdDownloadManager.enqueue(request);
//value = false;
}
}
if (value) {
view.loadUrl(url);
}
if (!url.contains("my site url")) { // Could be cleverer and use a regex
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
return false;
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
wv.loadUrl(mypage_error);
}
}
When i click any link containing above extension, the file starting download inside the webview app (It's OK) but at the same time that link automatically opening into my mobile browser. I know this is happening because of this code.
if (!url.contains("my site url")) { // Could be cleverer and use a regex
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
return false;
Now,
Firstly Solution I want know how can I download files containing above extension inside webview app and all link that don't contain above extension should open outside my webview app that means mobile browser.
Secondly Solution If it is not possible then tell me, when I click the link how to show alert/popup (browser choosing option) every time. As i mentioned instead of showing browser choosing alert links are opening automatically to my phone browser.
Upvotes: 1
Views: 1940
Reputation: 680
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean value = true;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
String mimeType = mime.getMimeTypeFromExtension(extension);
if (mimeType != null) {
if (mimeType.toLowerCase().contains("pdf")
|| extension.toLowerCase().contains("ppt")
|| extension.toLowerCase().contains("doc")
|| extension.toLowerCase().contains("rar")
|| extension.toLowerCase().contains("rtf")
|| extension.toLowerCase().contains("exe")
|| extension.toLowerCase().contains("apk")
|| extension.toLowerCase().contains("jpeg")
|| extension.toLowerCase().contains("png")
|| extension.toLowerCase().contains("xls")
|| extension.toLowerCase().contains("zip")
|| extension.toLowerCase().contains("jpg")) {
DownloadManager mdDownloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
String name= URLUtil.guessFileName(url,null,MimeTypeMap.getFileExtensionFromUrl(url));
File destinationFile = new File(Environment.getExternalStorageDirectory(),name);
request.setDescription("Downloading...");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
// request.setDestinationUri(Uri.fromFile(destinationFile));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,name);
mdDownloadManager.enqueue(request);
//value = false;
}
}
if (value) {
view.loadUrl(url);
}
if (!url.contains("my site url")) { // Could be cleverer and use a regex
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}else{
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Exit!")
.setMessage("Are you sure you want to close?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
return false;
}
return false;
}
Replace the positive and negative buttons code to do whatever you like. i hope this answers your use case.
Upvotes: 1