adplus-dvertising

Android Studio: How to get storage path from intent.Action_Open_Document_tree

Asked 3 months ago
Viewed 6 times

I am using an intent ACTION_OPEN_DOCUMENT_TREE which in activity results gives a path to folder. But that path is not correct to search file from specified folder.

Context: I am building an app that allows user to select directory where program will search for the file type but the path received from data.Getdata() is not correct. So any idea how will it can be done?

My code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
    //some lines of code
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
    intent.addCategory(Intent.CATEGORY_DEFAULT);                                       
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivityForResult(intent, 0);}
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data)
    if (data != null) {
        Uri uri_1= data.getData();

        String path=uri_1.getPath();;

        File custom_file=new File(path);
        //searching for specific file type in given folder 
        ArrayList<File> my_files= fetch_files(custom_file);

        //rest of code to what to do with files I received from fetch_files
       
    }

asked 3 months ago

Correct Answer

I am using an intent ACTION_OPEN_DOCUMENT_TREE which in activity results gives a path to folder

No, it does not. It gives you a Uri to a document collection.

the path received from data.Getdata() is not correct

It is not a filesystem path. It is not supposed to be a filesystem path. For example, https://stackoverflow.com/questions/74062576/android-studio-how-to-get-storage-path-from-intent-action-open-document-tree is a Uri, and /questions/74062576/android-studio-how-to-get-storage-path-from-intent-action-open-document-tree not a filesystem path on your phone.

If you want to do things with that Uri, call DocumentFile.fromTreeUri(), passing in your Uri, to get a DocumentFile representing that document tree. DocumentFile gives you an API that resembles that of File, but works with documents and trees that are part of the Storage Access Framework.

answered 3 months ago

Other Answer

It usually returns a path that starts with /tree/ so it needs to be converted into /storage/ path so that your files could be read. I found a code from a fellow member who did this (link:https://stackoverflow.com/a/65934369/20046611) but I modified it a little bit.

public String GetRealPath(Uri treeUri)
{
    if (treeUri == null)
        return "";
    String path1=treeUri.getPath();
    if (path1.startsWith("/tree/"))
    {
        String path2= path1.replace("/tree/","");
        if (path2.startsWith("primary:"))
        {
            String primary = path2.replace("primary:","");
            if (primary.contains(":"))
            {
                String storeName = "/storage/emulated/0/";
                String[] last = path2.split(":");
                String realPath = storeName + last[1];
                return realPath;
            }
            else{
                String storeName = "/storage/emulated/0/";
                String[] last = path2.split(":");
                String realPath = storeName + last[1];
                return realPath;
            }
        }
        else
        {
            if (path2.contains(":"))
            {
                String[] path3 = path2.split(":");
                String storeName = path3[0];
                String[] last = path2.split(":");
                String realPath = "/" + storeName + "/" + last[1];
                return realPath;
            }
        }
    }
    return path1;
}

all you need to do is enter String path=GetRealPath(uri_1); to get the path that will work.

answered 3 months ago