In my android Web view app I used code below to download file from app. It's working well and I can download file on my Samsung J2 (Android 5.2.2. API 22) ( but when I'm trying download any file from Symphony i10 ( Android 6. API 23) it “Unfortunately, … has stopped”. I didn't try with any other device. But I tried from Android Emulator ( virtual device API 24 and API 26). Same problem showing.
How can I solve this problem?
Main Activity:
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("jpeg")
|| extension.toLowerCase().contains("jpg")
|| extension.toLowerCase().contains("png")
) {
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 (!url.contains("my site url")) { // Could be cleverer and use a regex
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url)); // only used based on your example.
String title = "Select a browser";
// Create intent to show the chooser dialog
Intent chooser = Intent.createChooser(intent, title);
// Verify the original intent will resolve to at least one activity
// Verify the intent will resolve to at least one activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
return true;
}
return false;
}
return false;
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
wv.loadUrl(mypage_error);
}
});
Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" />
gradle:
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.my.app"
minSdkVersion 15
targetSdkVersion 25
versionCode 5
versionName '3.5'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
SDK
Note that my previous question is here like this: files will download mentioned above (extension) and except my site url all link should open mobile browser. Bu I didn't get answer that's why I used code below to warn before opening external link.
if (!url.contains("my site url")) { // Could be cleverer and use a regex
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url)); // only used based on your example.
String title = "Select a browser";
// Create intent to show the chooser dialog
Intent chooser = Intent.createChooser(intent, title);
// Verify the original intent will resolve to at least one activity
// Verify the intent will resolve to at least one activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
return true;
You can add Requesting Permissions at Run Time API 23+
Ref link:~
https://developer.android.com/training/permissions/requesting.html
Or Ref.link:~
https://www.androidhive.info/2016/11/android-working-marshmallow-m-runtime-permissions/