在某些设备上从 Android Webview 应用程序下载文件时出现“不幸的是,...已停止”
“Unfortunately, … has stopped” when Downloading file from Android Webview app in certain device
在我的 android Web 视图应用程序中,我使用下面的代码从应用程序下载文件。它运行良好,我可以在我的 Samsung J2 (Android 5.2.2. API 22) 上下载文件(但是当我尝试从 Symphony i10 下载任何文件时(Android 6. API 23) 它“不幸的是,...已经停止”。我没有尝试使用任何其他设备。但我尝试了 Android 模拟器(虚拟设备 API 24 和 API 26).同样的问题显示.
我该如何解决这个问题?
主要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);
}
});
清单:
<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
注意 my previous question is here 像这样:文件将下载上面提到的(扩展名),除了我的网站 url 所有 link 都应该打开移动浏览器。但是我没有得到答案,这就是为什么我在打开外部 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;
您可以在 运行 时间添加请求权限 API 23+
引用 link:~
https://developer.android.com/training/permissions/requesting.html
或Ref.link:~
https://www.androidhive.info/2016/11/android-working-marshmallow-m-runtime-permissions/
在我的 android Web 视图应用程序中,我使用下面的代码从应用程序下载文件。它运行良好,我可以在我的 Samsung J2 (Android 5.2.2. API 22) 上下载文件(但是当我尝试从 Symphony i10 下载任何文件时(Android 6. API 23) 它“不幸的是,...已经停止”。我没有尝试使用任何其他设备。但我尝试了 Android 模拟器(虚拟设备 API 24 和 API 26).同样的问题显示.
我该如何解决这个问题?
主要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);
}
});
清单:
<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
注意 my previous question is here 像这样:文件将下载上面提到的(扩展名),除了我的网站 url 所有 link 都应该打开移动浏览器。但是我没有得到答案,这就是为什么我在打开外部 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;
您可以在 运行 时间添加请求权限 API 23+
引用 link:~
https://developer.android.com/training/permissions/requesting.html
或Ref.link:~
https://www.androidhive.info/2016/11/android-working-marshmallow-m-runtime-permissions/