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 ?
You should use ContentResolver to open resource URIs: Uri uri = Uri. parse("android. resource://your.package.here/drawable/image_name"); InputStream stream = getContentResolver().
For example, when creating a state list drawable, you can reference a color resource for the android:drawable attribute ( android:drawable="@color/green" ).
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.? } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With