gpt4 book ai didi

android - “Unfortunately, … has stopped” 在某些设备上从 Android Webview 应用程序下载文件时

转载 作者:行者123 更新时间:2023-12-03 16:24:27 32 4
gpt4 key购买 nike

在我的 android Web View 应用程序中,我使用下面的代码从应用程序下载文件。它运行良好,我可以在我的三星 J2(Android 5.2.2. API 22)上下载文件(但是当我尝试从 Symphony i10(Android 6. API 23)下载任何文件时,它“不幸的是,......已停止”。我没有尝试使用任何其他设备。但我尝试了 Android Emulator(虚拟设备 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" />

毕业典礼:
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

enter image description here

请注意 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;

最佳答案

关于android - “Unfortunately, … has stopped” 在某些设备上从 Android Webview 应用程序下载文件时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45771972/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com