0

I have found this snippet of code:

    @TargetApi(Build.VERSION_CODES.KITKAT)
    public static String getPath(final Context context, final Uri uri) {

        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
        if (isKitKat) {
            // ExternalStorageProvider
            if (isExternalStorageDocument(uri)) {
                final String docId = uri.getPath();
                final String[] split = docId.split(":");
                final String type = split[0];

                if ("/tree/primary".equalsIgnoreCase(type)) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                }                
            }           
        }
        return null;
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is ExternalStorageProvider.
     */
    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

I want to use it to translate uris to full paths, and I want more information about what I can get as "authority", but in the official documentation I only find how to create an authority, not information about what can I expect to find on a normal device.

There is even more complex code like this one from this question but I would like to find official documentation behind this kind of logic. A good explanation of how this works is also welcomed.

Josu Goñi
  • 1,178
  • 1
  • 10
  • 26
  • 1
    You should never be doing anything that those questions are talking about: you can do everything you actually need to do without anything like that by working with the Uri directly. It sounds like this is an [XY Problem](https://xyproblem.info/) - what is the underlying problem you are trying to solve? – ianhanniballake May 22 '22 at 21:28
  • My app manages all files. To pick a directory I either build my own file picker, or use the one provided by Android, which returns an uri. But my app works with full paths at this moment. I might be able to refactor it to use the new permissions but I'm not sure if I want to do that and it is a lot of work right now. I'm just replacing the directory picker library that stopped working when I bumped the React Native version. – Josu Goñi May 22 '22 at 21:56
  • It is not an XY problem anyway because I'm interested on this regardless of what I end up using. – Josu Goñi May 22 '22 at 21:58
  • 1
    "My app manages all files" -- your code is unrelated to files. "I either build my own file picker, or use the one provided by Android" -- the one provided by Android is not a file picker, assuming that you are referring to Storage Access Framework options like `ACTION_OPEN_DOCUMENT_TREE`. That tree could be anywhere (e.g., a cloud storage provider, a network file server, an encrypted document store); it is not limited to filesystem directories. – CommonsWare May 22 '22 at 22:00
  • That is why I want to learn about that: to inform the user that the app can't and won't deal with folders from cloud storage providers, etc, should they pick one. I want to allow the user to pick a folder on the external storage and I want the user to be able to change or syncronize that folder using other apps like a file manager or DropSync etc. – Josu Goñi May 22 '22 at 22:11
  • "to inform the user that the app can't and won't deal with folders from cloud storage providers, etc, should they pick one" -- you do not have a reliable way of distinguishing one from another. "I want the user to be able to change or syncronize that folder using other apps like a file manager or DropSync" -- why does it matter where the folder resides? Why does it *have* to be a filesystem directory? If the user wants to synchronize a local encrypted document store, why does it matter to you? It is the user's phone and the user's data. – CommonsWare May 22 '22 at 22:16
  • 1
    I hope you've realized that this is exactly an XY Problem - your actual problem ("I want the user to be able to change or syncronize that folder using other apps like a file manager or DropSync") is not solved by deeply inspecting the contents of the Uris returned to you - that's precisely the second bullet point of the site I linked to you. Do you want to ask about how to synchronize a folder returned by `ACTION_OPEN_DOCUMENT_TREE` with another folder? – ianhanniballake May 22 '22 at 22:20
  • @CommonsWare It matters to me because at this moment the app is designed and tested to work with normal files. An uri from a remote folder might crash de app if there is no network as I expect to be able to read all the files in the folders that the user has selected. At this point I'm the only user so it is not a big problem but I think I have to either ditch ACTION_OPEN_DOCUMENT_TREE and do my own picker or learn and refactor to use the uri based apis. An official documentation might give me guarantees that I can keep working with the intent. – Josu Goñi May 22 '22 at 23:05

1 Answers1

2

I want to use it to translate uris to full paths

That will not be reliable. A Uri is an opaque identifier, nothing more.

I want more information about what I can get as "authority", but in the official documentation I only find how to create an authority, not information about what can I expect to find on a normal device

That is because this information varies by individual device:

  • OS version
  • Device manufacturer
  • User-installed apps
  • App versions

None of it is documented, because it is all internal implementation of apps (from the core OS, devices, and user-installed apps).

I would like to find official documentation behind this kind of logic

There is none.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So apparently I can't rely on `com.android.externalstorage.documents` indicating that the file belongs to `Environment.getExternalStorageDirectory()` etc that the SO questions about this show as an answer. – Josu Goñi May 22 '22 at 22:44
  • 1
    @JosuGoñi: Correct. In many of those SO questions, you will see comments or other answers from me, pointing out that their approaches are flawed. – CommonsWare May 22 '22 at 22:46