简体   繁体   English

无法使用模拟器中的下载管理器下载文件

[英]Unable to download files using download manager in emulator

Android Studio 2.1.2, API 23 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. java.lang.SecurityException:不允许写入/存储/仿真/0/Download/aabd.pdf:用户10059或当前进程都没有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" />

You have to check permission like this if you have targetSdk 23 如果您有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;
        }
    }
}

Is sd card emulation is enabled in emulator? 是否在模拟器中启用了SD卡模拟功能? you may want to use Genymotion emulator instead of built-in 您可能要使用Genymotion模拟器而不是内置的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM