How to get URI from Drawable

I am working on an Android application, where I supply some embedded images and give the user the option to download a few more from the Internet for use in the application. At some point in my application, I look at the ImageView in my layout and want to determine if the Drawable inside is an embedded resource or an image that I downloaded from the Internet to an SD card.

Is there a way to extract the Drawable URI used in ImageView? That way, I can see if this is a resource or a downloaded file.

Here is my code:

ImageView view = (ImageView) layout.findViewById(R.id.content_img); Drawable image = view.getDrawable(); 

UPDATE: Using the Barry Fruitman suggestion from below, I saved the image URI directly inside my custom ImageView for later use. This is what my implementation looks like:

 public class MemoryImageView extends ImageView { private String storedUri = null; public MemoryImageView(Context context, String Uri) { super(context); } public MemoryImageView(Context context, AttributeSet attrs) { super(context, attrs); } public MemoryImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public String getStoredUri() { return storedUri; } public void setStoredUri(String storedUri) { this.storedUri = storedUri; } 

}

And the use looks like this:

 MemoryImageView view = (MemoryImageView) layout.findViewById(R.id.content_img); String img = view.getStoredUri(); if(img.startsWith("android.resource")) { //in-built resource } else { //downloaded image } 
+4
source share
1 answer

No. Once you create Drawable, this information will be lost. I suggest you subclass ImageView and add extra member (s) to keep track of everything you want.

Replace:

 <ImageView /> 

with

 <com.mypackage.MyImageView /> 

And create:

 class MyImageView extends ImageView { protected final int LOCAL_IMAGE = 1; protected final int REMOTE_IMAGE = 2; protected int imageType; } 

MyImageView will behave exactly the same as ImageView, but with this extra member that you can read and write anywhere. You may also have to override the ImageView constructor with constructors that simply call super ().

+3
source

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


All Articles