Download files to Webview on android 6.0

Asked

Viewed 448 times

0

I am using the following code to download files in web view:

        @SuppressLint("InlinedApi")public void onDownloadStart(String url,String userAgent,String contentDisposition,String mimetype,long contentLength){
            DownloadManager.Request request =new DownloadManager.Request(Uri.parse(url));                
            request.allowScanningByMediaScanner();final String filename =URLUtil.guessFileName(url, contentDisposition, mimetype);          
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);               
            request.setDestinationInExternalPublicDir("/Download", filename);DownloadManager dm =(DownloadManager) getSystemService(DOWNLOAD_SERVICE);          
            dm.enqueue(request);Intent intent =new Intent(Intent.ACTION_OPEN_DOCUMENT);             
            intent.addCategory(Intent.CATEGORY_OPENABLE);    
            intent.setType("*/*");
            Toast.makeText(getApplicationContext(),"Baixando",
            Toast.LENGTH_LONG).show();}}); //

The same is working normally on Android 4.0 ICS, but on Android 6.0 nothing happens. In this case, it would be necessary to perform downloads on Android 6.0?

1 answer

0


I think your problem is that from API 23 the permissions should be checked before the action happens, not just put in the manifest.xml.

This function can help you:

public  boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
    if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {
        Log.v(TAG,"Permission is granted");
        return true;
    } else {

        Log.v(TAG,"Permission is revoked");
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        return false;
    }
}
else { //permission is automatically granted on sdk<23 upon installation
    Log.v(TAG,"Permission is granted");
    return true;
}

}

I hope I can help. Hugs.

  • Thank you Marcelo, after your answer I really believe that this is it. I’m trying to add your code to mine but I’m having some problems and I had to modify it a lot, I’m afraid it won’t work right away, but I’m already accepting your answer as correct. Thanks.

  • Of course...you will have to adapt mainly if you are using Ragment but this is the core.... hopefully it can work, at least it’s a start so you can search for more information. Good luck.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.