Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read or write file as getExternalStorageDirectory is deprecated in API 29?

I am learning android development and i am facing some problem to read getExternalStorageDirectory in java, I have read https://developer.android.com/reference/android/os/Environment but can't understand, can someone help me with example code in java.

like image 525
Mujahid Khan Avatar asked Jun 29 '19 17:06

Mujahid Khan


2 Answers

From the docs you can see:

getExternalStoragePublicDirectory(String type)

This method was deprecated in API level 29. To improve user privacy, direct access to shared/external storage devices is deprecated. When an app targets Build.VERSION_CODES.Q, the path returned from this method is no longer directly accessible to apps. Apps can continue to access content stored on shared/external storage by migrating to alternatives such as Context#getExternalFilesDir(String), MediaStore, or Intent#ACTION_OPEN_DOCUMENT.

Pass nothing as parameter to this function to get your directory as a File object :

context.getExternalFilesDir();

Here "Context" is an object which is obtained by this.getContext();

this is the current object of the Activity. Do check the scope carefully while using it.

Important

To access the Internal storage, Manifest.permission.WRITE_EXTERNAL_STORAGE and/or Manifest.permission.READ_EXTERNAL_STORAGE are required in the file AndroidManifest.xml.

Optional information:

  1. Usually the internal storage has the path /sdcard/ on Android devices. It's not a real path but a symlink.

  2. It's confusing but "external sdcard" in Android acutally means the Internal device storage and not the external ejectable out-of-the-device memory card storage. Also note that the real external sdcard cannot be fully access

  3. Activity class extends the Context class That's why we can get the context from it.

like image 123
crazo7924 Avatar answered Oct 05 '22 06:10

crazo7924


Updated

From Android 11, it won't allow creating folder/file in the root directory but, we can still manage folder separation with help of the public directory (It will show a deprecated warning but it will work)

fun getAbsolutePath(context: Context): File {
    return File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "{YOUR_FOLDER_NAME}"))
}

Now, create the file with common directory path

val file = File(getAbsolutePath(requireContext()), "FILENAME.EXTENSION")

Older

Use this static method. Currently I don't find any legal way to do this. So, I was made this static method to get root or getAbsolutePath file path.

public static File getAbsoluteDir(Context ctx, String optionalPath) {
        String rootPath;
        if (optionalPath != null && !optionalPath.equals("")) {
            rootPath = ctx.getExternalFilesDir(optionalPath).getAbsolutePath();
        } else {
            rootPath = ctx.getExternalFilesDir(null).getAbsolutePath();
        }
        // extraPortion is extra part of file path
        String extraPortion = "Android/data/" + BuildConfig.APPLICATION_ID
                + File.separator + "files" + File.separator;
        // Remove extraPortion
        rootPath = rootPath.replace(extraPortion, "");
        return new File(rootPath);
    }
like image 26
Niral Dhameliya Avatar answered Oct 05 '22 07:10

Niral Dhameliya