If we want to load image from asset folder and we need to convert that image into drawable, than we just need to call this method loadImageFromAsset() and pass the name of the image as a parameter at your activity class, it will convert it into drawable image and return it to you.
String imageName="splash";
Drawable drawable = loadImageFromAsset(imageName);
if (drawable != null) {
// set it wherever you want.
Log.d("TheActivity", "Setting the background");
}
public Drawable loadImageFromAsset(String imageName) {
Drawable drawable;
// load image
try {
// get input stream
InputStream ims = getAssets().open(imageName+".png");
//Note: Images can be in hierarical
// load image as Drawable
drawable = Drawable.createFromStream(ims, null);
}
catch(IOException ex) {
Log.d("LoadingImage", "Error reading the image");
return null;
}
return drawable;
}
0 Comment(s)