Android - getting image resource in Uri: IllegalArgumentException

I am trying to load an image from a resource in my Android application, but I keep getting the error "Error in IllegalArgumentException: expected file in URI".

I put the file 'missing_album_art.png' into the drawable-hdpi, drawable-ldpi and drawable-mdpi files in my project and updated Eclipse to display them there.

The line I use to create the Uri is:

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

"android.aberplayer" is the main package of my application, as well as the one that contains the class in which I use the above line.

The error I am 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 image resources in Android apps?

+6
source share
3 answers

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

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

Note that Uri.parse used to get Uri from an Url string not from a resource directly. There is another pure solution, but I could not remember them. I will update the message when I find them.

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

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

Mark this post.

+8
source

All resources are mapped to integers. You want to get the integer value R.drawable.missing_album_art, and then add this to the rest of the line of your path. i.e.,

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

... that is, if you really need Uri. You can usually use an image resource as follows:

 view.setImageResource(R.drawable.missing_album_art); 

If you want to read the resource in a file, this is one way to do this (it writes the file to the application directory (for example, / 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; } 
+3
source

Try this instead

 Uri uri = Uri.parse("R.drawable.missing_album_art"); 
-3
source

Source: https://habr.com/ru/post/910857/


All Articles