7

I'm trying to load an image from the resource in my Android app but I keep getting the "IllegalArgumentException: Expected file scheme in URI" error.

I put my 'missing_album_art.png' file in drawable-hdpi, drawable-ldpi and drawable-mdpi folders in my project and refreshed Eclipse so that it displays they're there.

The line I'm using to create the Uri is:

Uri uri = Uri.parse("android.resource://android.aberplayer/R.drawable.missing_album_art");

The 'android.aberplayer' is the main package of my application and also the one containing the class in which I use the above line.

The error I keep getting is:

03-16 00:16:29.252: E/AndroidRuntime(25598): FATAL EXCEPTION: main
03-16 00:16:29.252: E/AndroidRuntime(25598): java.lang.IllegalArgumentException: Expected file scheme in URI: android.resource://src.android.aberplayer/R.drawable.missing_album_art
03-16 00:16:29.252: E/AndroidRuntime(25598):    at java.io.File.checkURI(File.java:245)
03-16 00:16:29.252: E/AndroidRuntime(25598):    at java.io.File.<init>(File.java:182)
03-16 00:16:29.252: E/AndroidRuntime(25598):    at android.aberplayer.CurrentSongList.getCurrentAlbumArt(CurrentSongList.java:69)

Is there something I don't understand about accessing the image resources in Android applications?

JakeP
  • 1,736
  • 4
  • 23
  • 31

3 Answers3

9

If you can put the image file into assets folder, the code below can be used to get the Uri -

Uri uri=Uri.parse("file:///android_asset/images/missing_album_art.png");

Note that Uri.parse is used for getting Uri from a string Url not from a resource directly. There are other cleaner solution, but I couldn't recall them. I'll update the post when I found them.

Edit: The String Uri path should be in the following format if you want to use the resource directly:-

"android.resource://[package]/[res type]/[res name]"

Check this post.

0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184
  • Just a reminder that the constant in the framework is: `ContentResolver.SCHEME_ANDROID_RESOURCE` – Simon Nov 22 '14 at 02:50
3

The resources all get mapped to integers. You want to get the integer value of R.drawable.missing_album_art, then append that to the rest of your path string. i.e.,

Uri uri = Uri.parse("android.resource://android.aberplayer/" + R.drawable.missing_album_art);

…that is, if you really do need the Uri. Normally, you can just use an image resource like this:

view.setImageResource(R.drawable.missing_album_art);

If you want to read a resource into a file, the following is one way to do it (this writes a file to the application directory (i.e. /data/data/packagename) with the name "filename"):

private File resToFile(int resourceID, String filename) {
    File file = getApplicationContext().getFileStreamPath(filename);
    if(file.exists()) {
        return file;
    }

    InputStream is;
    FileOutputStream fos;
    try {
        is = getResources().openRawResource(resourceID);
        byte[] buffer = new byte[is.available()];
        is.read(buffer);
        fos = openFileOutput(filename, MODE_PRIVATE);
        fos.write(buffer);
        fos.close();
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return file;
}
  • I changed the code to append the R.drawable.missing_album_art to the string but the problem persists, only now the error line is `java.lang.IllegalArgumentException: Expected file scheme in URI: android.resource://src.android.aberplayer/2130837505` – JakeP Mar 16 '12 at 11:23
  • Is it possible that there is a problem because I'm trying to use the abovementioned Uri.parse line in a different class than an activity? When I used it in the Activity which is supposed to display the picture, it worked. However, when I try to use it in a different class, which doesn't extend or implement any Android classes, it results in an error. Can you explain this for me please? (Do you need to see any code?) – JakeP Mar 16 '12 at 22:51
  • @KurakDarcia hmm. To be honest I am not sure. I feel like the error isn't coming from that `Uri.parse` line. Maybe post some more context/lines of code. Why are you using a Uri/how are you using it? – Jonathan Fretheim Mar 17 '12 at 20:27
  • just saw your comment below that the error actually comes from `currentAlbumArt = new File(new URI(uri.toString()));` modified answer with a method for writing a resource to a file. Still not sure if that's what you're looking for. – Jonathan Fretheim Mar 17 '12 at 20:47
-3

Try this instead

Uri uri = Uri.parse("R.drawable.missing_album_art");
LuizAurio
  • 157
  • 1
  • 7
  • This results in "IllegalArgumentException: URI is not absolute: R.drawable.missing_album_art" when I try to convert the Uri into File with this: `currentAlbumArt = new File(new URI(uri.toString()));` – JakeP Mar 16 '12 at 11:19