简体   繁体   中英

Android - Reference resource drawable as a URL

I'm using an API in my Android app that downloads images from the web and shows a placeholder animation while the images are downloading (RemoteImageView, part of the Ignition package here .

At some points in the app, I need to show images from the local resource drawables and not downloaded from the web.

Is it possible to access the local resource drawables using a URL ?

The way to do it:

use this line "android.resource://[your package]/"+[res id]

Here is an example

Uri myURI = Uri.parse("android.resource://com.example.project/" + R.drawable.myimage);

for convenient you can use it as a method:

public static Uri getUrl(int res){
  return Uri.parse("android.resource://com.example.project/" + res);
}

Warning do not use context.getPackageName() as substitute to package name because it might return a null value.

Local image resources do not have urls, they have URIs. So if you have image in drawable, you can parse them from resource id to URI.

Uri uri=Uri.parse("R.drawable.image");

However, if you can also put your images in asset folder of the package and access them using their URL. The URL of the image files would be "file:///android_asset/image.png" You can use either of the option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM