Skip to content Skip to sidebar Skip to footer

Android How To Get The Root Directory From A File (path)

How do I get the root directory of any path. If i have file path /storage/emulated/0/Android/media/com.google.android.talk/Ringtones/, what is the best way for me to return the roo

Solution 1:

What you are suggesting would work. One way would be to use substring with indexOf.

rootPath = string.substring(0, string.indexOf("/")); 

Solution 2:

You can access the root folder from ".apk" as follows:

java.io.File dir = new java.io.File("/storage/emulated/0");

and, can create subfolders with:

voidCreateDir()throws IOException {
    java.io.Filedir=newjava.io.File("/storage/emulated/0/MyApplication");
    dir.mkdir();
}

Normally, you must have write permissions to do this:

<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

But the problem starts with Android 10 (API level 29) and newer. Each path is null.

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.

Post a Comment for "Android How To Get The Root Directory From A File (path)"