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?
Try this instead
If you can put the image file into
assets
folder, the code below can be used to get theUri
-Note that
Uri.parse
is used for gettingUri
from a stringUrl
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:-
Check this post.
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.,
…that is, if you really do need the Uri. Normally, you can just use an image resource like this:
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"):