Download files in WebView on android 6.0

0

I am using the following code to download files in the 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 that case, what would it take to make downloads on Android 6.0?

    
asked by anonymous 31.07.2016 / 18:57

1 answer

0

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

This feature 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;
}

}

Hope I can help. Hugs.

    
03.08.2016 / 15:09