How to check for a local Drawable, and lacking one, how to make one from a remote resource? [closed]
-
09-06-2021 - |
Pregunta
I need to take a given drawable name from a cloud DB and check to see if a matching Drawable exists in the local package. If it doesn't, I need to create a local copy of the image for use so I'm not constantly accessing the remote resource. What's the best way to do this? I can access the database, do the SQL, all that stuff. I just need to know how to A) check the local package for the existence of a Drawable called (for instance) 'icon,' and if it doesn't exist locally I need to know how to create and store a local copy (via HTTPstream maybe?).
Solución
1: Check for your drawable like this:
int resID = getResources().getIdentifier("icon", "drawable", "your.package.namespace");
if (resId == 0){
//drawable isn't there. You have to fetch it through Http or whatever
} else {
//getDrawable like this
Drawable d = getResources.getDrawable(resId);
}
2: You can't put a downloaded drawable inside drawableFolder. But you can download it to res/raw or to the sdcard. Note to check that location also if you already downloaded this particular drawable.