Giuseppedes
Giuseppedes

Reputation: 137

check uri for android raw file

i'm trying to get the URI of a file in the raw folder of my app in this way:

Uri myUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.myfile);

or

Uri myUri = Uri.parse("android.resource://mypackage/raw/myfile");

Then i use this method to check if the uri was correct:

File f = new File(myUri.toString());
    if (f.exists()) {
        Log.d("YES", "file exist");
    }
    if (!f.exists()){
        Log.d("NO", "file doesn't exist");
    }

But i always get: "No, file doesn't exist".

Do you know why?

Upvotes: 2

Views: 1177

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006819

First, because there is no file. Resources are files on your developer machine. They are not files on the device.

Second, because new File(myUri.toString()) will never work. A Uri always has a scheme; filesystem paths never have a scheme.

Third, because even if you used new File(myUri.getPath()), that will work at most for a file:// Uri. There are many other types of Uri schemes; none of them will have paths that map directly to filesystem paths. Even with a file:// Uri, the filesystem path may not be one that you have direct access to (e.g., an arbitrary path on removable media on Android 4.4+).

Upvotes: 2

Related Questions