在我的Android Web视图应用程序中,我使用下面的代码从app下载文件。它运行良好,我可以在我的Samsung J2(Android 5.2.2.API 22)上下载文件(但是当我尝试从Symphony i10(Android 6.API 23)下载任何文件时,它“很遗憾” ,...已停止“。我没有尝试使用任何其他设备。但我尝试使用Android Emulator(虚拟设备API 24和API 26)。显示同样的问题。
我该如何解决这个问题?
主要活动:
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这样:文件将在上面提到(扩展名),除了我的网站网址,所有链接都应该打开移动浏览器。我没有得到答案,这就是为什么我在打开外部链接之前使用下面的代码进行警告的原因。
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;
答案 0 :(得分:2)
您可以在运行时API 23 +
添加请求权限参考链接:〜
https://developer.android.com/training/permissions/requesting.html
或Ref.link:~
https://www.androidhive.info/2016/11/android-working-marshmallow-m-runtime-permissions/