Android - getting an image resource into Uri: Ille

2020-02-26 10:05发布

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?

3条回答
女痞
2楼-- · 2020-02-26 10:25

Try this instead

Uri uri = Uri.parse("R.drawable.missing_album_art");
查看更多
该账号已被封号
3楼-- · 2020-02-26 10:38

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.

查看更多
够拽才男人
4楼-- · 2020-02-26 10:38

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;
}
查看更多
登录 后发表回答