Search code examples
androidandroid-contentresolver

How to differentiate between a file URI and contentprovider URI?


Suppose I'm selecting a file externally via:

Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, GALLERY_REQUEST_CODE);

and in the result is returned via onActivityResult

Uri file = data.getData();

This can be in different formats. What is the convention here to determine if a content resolver query is needed? If the file URI is a file path file:/// or a app URI like //package.example/....

I know I could check if the uri has a file:/// prefix but that seems a bit hacky. I just want the file path.


Solution

  • What is the convention here to determine if a content resolver query is needed?

    openInputStream() on ContentResolver handles both the file scheme and the content scheme.

    I know I could check if the uri has a file:/// prefix but that seems a bit hacky

    Um, well, Uri has a getScheme() method.

    I just want the file path.

    There is no "file path" for a content Uri, as there is no requirement that the content be stored as a file.