Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Reference resource drawable as a URL

Tags:

android

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 ?

like image 514
Saad Farooq Avatar asked Feb 15 '12 16:02

Saad Farooq


People also ask

How do I find the URL of a drawable image?

You should use ContentResolver to open resource URIs: Uri uri = Uri. parse("android. resource://your.package.here/drawable/image_name"); InputStream stream = getContentResolver().

How do you reference drawable Android?

For example, when creating a state list drawable, you can reference a color resource for the android:drawable attribute ( android:drawable="@color/green" ).

How do you get a drawable resource from a name?

public class DrawableManager { private static Context context = null; public static void init(Context c) { context = c; } public static Drawable getDrawable(String name) { return R. drawable.? } }


2 Answers

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.

like image 83
neferpitou Avatar answered Nov 15 '22 18:11

neferpitou


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.

like image 32
noob Avatar answered Nov 15 '22 16:11

noob