“;不幸的是,;已停止”;在特定设备上从Android Webview应用程序下载文件时

“;不幸的是,;已停止”;在特定设备上从Android Webview应用程序下载文件时,android,crash,android-webview,android-download-manager,Android,Crash,Android Webview,Android Download Manager,在我的android Web view应用程序中,我使用下面的代码从应用程序下载文件。它运行良好,我可以在我的三星J2(Android 5.2.2.API 22)上下载文件(但当我尝试从Symphony i10(Android 6.API 23)下载任何文件时,“不幸的是,…已停止”。我没有尝试使用任何其他设备。但我尝试了从Android Emulator(虚拟设备API 24和API 26)上下载。显示相同的问题 我怎样才能解决这个问题 主要活动: wv.setWebViewClient(ne

在我的android Web view应用程序中,我使用下面的代码从应用程序下载文件。它运行良好,我可以在我的三星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" />
SDK

请注意,像这样:文件将下载上述(扩展名)和除了我的网站url所有链接应打开移动浏览器。但我没有得到答案,这就是为什么我使用下面的代码警告之前打开外部链接

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中添加请求权限+

参考链接:~

或参考链接:~


您可以在运行时API 23中添加请求权限+

参考链接:~

或参考链接:~

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;