17

我正在使用下载管理器,当我使用

 setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, "example.ext");

文件下载到 Android/data/com.example.app/files/Download 文件夹。

当我尝试

setDestinationInExternalPublicDir("/folder", "example.ext");

我得到: IllegalStateException: Cannot create directory mnt/sdcard/folder。我也设置了 WRITE_EXTERNAL_STORAGE 权限。

我究竟做错了什么?

4

5 回答 5

2

在清单文件中添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
于 2018-07-31T13:57:55.510 回答
1

利用request.setDestinationInExternalPublicDir("/folder", "FileName.extention");

这对我有用..

于 2013-10-31T10:56:15.073 回答
1

为什么不使用absoluteExternalFileDir 的路径

File sdCard = Environment.getExternalStorageDirectory();
String folder = sdCard.getAbsolutePath() + "/YourFolder" ;
File dir = new File(folder );
if (!dir.exists()) {
if (dir.mkdirs()) {
    Log.i(Tag,"Directory  Created");
    }
}

我想这甚至可能对你有用。

于 2013-05-25T13:14:45.907 回答
1

我使用以下命令解决了我的问题:

request.setDestinationUri(Uri.fromFile(new File(destinationDirectory,fileName + fileExtension))); 

目的地目录在哪里

String destinationDirectory = Environment.getExternalStorageDirectory() + "/MyFolder/"
于 2019-01-24T23:15:29.260 回答
0

检查权限,如

//区域[权限]

public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static boolean CheckPermission(final Context context){
    int currentAPIVersion = Build.VERSION.SDK_INT;
    if( currentAPIVersion >= Build.VERSION_CODES.M){
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
                alertBuilder.setCancelable(true);
                alertBuilder.setTitle("Permission necessary");
                alertBuilder.setMessage("External storage permission is necessary");
                alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                    public void onClick(DialogInterface dialog, int which) {
                        ActivityCompat.requestPermissions((Activity) context,
                                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
                    }
                });
                AlertDialog alert = alertBuilder.create();
                alert.show();
            } else {
                ActivityCompat.requestPermissions((Activity) context,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
            }
            return false;
        } else {
            return true;
        }
    } else {
        return true;
    }
}

//endregion
于 2018-07-31T14:03:29.153 回答