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 }
source share