Unable to download files using download manager in emulator

随声附和 提交于 2019-12-12 03:02:34

问题


Android Studio 2.1.2, API 23

    Error:

java.lang.SecurityException: No permission to write to/storage/emulated/0/Download/aabd.pdf: Neither user 10059 nor current process has android.permission.WRITE_EXTERNAL_STORAGE.

Code :

     File file = new File(Environment.getExternalStoragePublicDirectory
            (Environment.DIRECTORY_DOWNLOADS), nameOfFile);
   request.setDestinationInExternalPublicDir
  (Environment.DIRECTORY_DOWNLOADS, nameOfFile);
   request.setVisibleInDownloadsUi(true);
   myDownloadReference = downloadManager.enqueue(request);

In the devices, it is working fine.

In Manifest permission is there

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.player">


<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

回答1:


You have to check permission like this if you have targetSdk 23

    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
        checkPermission();
    }
    else {

    File file = new File(Environment.getExternalStoragePublicDirectory
        (Environment.DIRECTORY_DOWNLOADS), nameOfFile);
            request.setDestinationInExternalPublicDir
        (Environment.DIRECTORY_DOWNLOADS, nameOfFile);
        request.setVisibleInDownloadsUi(true);
        myDownloadReference = downloadManager.enqueue(request);
    }


private void checkPermission() {
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {//Can add more as per requirement

                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},
                        123);

            } else {

            }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 123: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
         File file = new File(Environment.getExternalStoragePublicDirectory
        (Environment.DIRECTORY_DOWNLOADS), nameOfFile);
            request.setDestinationInExternalPublicDir
        (Environment.DIRECTORY_DOWNLOADS, nameOfFile);
        request.setVisibleInDownloadsUi(true);
        myDownloadReference = downloadManager.enqueue(request);

            } else {

                checkPermission();
            }
            return;
        }
    }
}



回答2:


Is sd card emulation is enabled in emulator? you may want to use Genymotion emulator instead of built-in



来源:https://stackoverflow.com/questions/38010972/unable-to-download-files-using-download-manager-in-emulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!