0

helo im using android webview, i try to open file after download with this code : but have error when opening file , something wrong on my code , any one have idea ? i think my problem are on this line:

  intent.setDataAndType(Uri.parse(request.toString()), "application/*");

//handle downloading

        webview.setDownloadListener(new DownloadListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                    if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                        request.setMimeType(mimeType);
                        String cookies = CookieManager.getInstance().getCookie(url);
                        request.addRequestHeader("cookie", cookies);
                        request.addRequestHeader("User-Agent", userAgent);
                        request.setDescription("Downloading file....");
                        request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
                        request.allowScanningByMediaScanner();
                        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimeType));
                        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                        dm.enqueue(request);

                        
                            Intent intent = new Intent();
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.setAction(Intent.ACTION_VIEW);
                            intent.setDataAndType(Uri.parse(request.toString()), "application/*");
                            startActivity(intent);

                            
                    } else {
                        Log.d("permission", "permission denied to WRITE_EXTERNAL_STORAGE - requesting it");
                        String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
                        requestPermissions(permissions, 1);
                    }
                }
            }



        });

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

The code is working fine. With an emulator I get the same error with opening file. But with the a real device it working fine. Ergo: The emulator is having issues. Try to update it and restart everything.

Ole Pannier
  • 3,208
  • 9
  • 22
  • 33