Can't create directory in Android 10

后端 未结 5 1620
感情败类
感情败类 2020-11-22 08:38

I\'m unable to create directory in android 10. It\'s working on devices till android Oreo.

I tried two ways for creating folders.

Using

相关标签:
5条回答
  • 2020-11-22 09:11

    For Android 10, you can add

    android:requestLegacyExternalStorage="true"

    to your element in the manifest. This opts you into the legacy storage model, and your existing external storage code will work. This fix will not work on Android R and higher though, so this is only a short-term fix.

    0 讨论(0)
  • 2020-11-22 09:18

    only use

    android:requestLegacyExternalStorage="true"
    

    in manifests

    0 讨论(0)
  • 2020-11-22 09:27

    Since Q beta 4 it's possible to opt-out of that feature by:

    targeting api 28 (or lower) using requestLegacyExternalStorage manifest attribute:

    <manifest ... >
      <!-- This attribute is "false" by default on apps targeting Android Q. -->
      <application android:requestLegacyExternalStorage="true" ... >
        ...
      </application>
    </manifest>
    
    0 讨论(0)
  • 2020-11-22 09:32

    As was first disclosed back in March 2019, you no longer have access by default to arbitrary locations on external storage or removable storage on Android 10+. This includes Environment.getExternalStorageDirectory() and other methods on Environment (e.g., getExternalStoragePublicDirectory().

    For Android 10 and 11, you can add android:requestLegacyExternalStorage="true" to your <application> element in the manifest. This opts you into the legacy storage model, and your existing external storage code will work.

    Otherwise, your choices are:

    • Use methods on Context, such as getExternalFilesDir(), to get at directories on external storage into which your app can write. You do not need any permissions to use those directories on Android 4.4+. However, the data that you store there gets removed when your app is uninstalled.

    • Use the Storage Access Framework, such as ACTION_OPEN_DOCUMENT and ACTION_CREATE_DOCUMENT.

    • If your content is media, you can use MediaStore to place the media in standard media locations.

    0 讨论(0)
  • 2020-11-22 09:34

    This works for me and I think it's functional on Android 10>

                ContentResolver resolver = getContentResolver();
                ContentValues contentValues = new ContentValues();
                contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/Folder Example");
                String path = String.valueOf(resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues));
                File folder = new File(path);
                boolean isCreada = folder.exists();
                if(!isCreada) {
                    folder.mkdirs();
                }
    
    0 讨论(0)
提交回复
热议问题