I would like to set a certain Drawable
as the device's wallpaper, but all wallpaper functions accept Bitmap
s only. I cannot use WallpaperManager
because I'm pre 2.1.
Also, my drawables are downloaded from the web and do not reside in R.drawable
.
Source: Tips4all
This converts a Drawable to a Bitmap.
ReplyDeleteDrawable d = ImagesArrayList.get(0);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
This piece of code helps.
ReplyDeleteBitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon_resource);
Edit: Here a version where the image gets downloaded.
String name = c.getString(str_url);
URL url_value = new URL(name);
ImageView profile = (ImageView)v.findViewById(R.id.vdo_icon);
if (profile != null) {
Bitmap mIcon1 =
BitmapFactory.decodeStream(url_value.openConnection().getInputStream());
profile.setImageBitmap(mIcon1);
}
A Drawable can be drawn onto a Canvas, and a Canvas can be backed by a Bitmap:
ReplyDeleteBitmap bmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
Maybe this will help someone...
ReplyDeleteFrom PictureDrawable to Bitmap, use:
private Bitmap pictureDrawableToBitmap(PictureDrawable pictureDrawable){
Bitmap bmp = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.drawPicture(pictureDrawable.getPicture());
return bmp;
}
... implemented as such:
Bitmap bmp = pictureDrawableToBitmap((PictureDrawable) drawable);
/**
ReplyDelete* This method returns a bitmap related to resource id. It is ready to use method, you can
* use it by simply copying in your project.
*
* @param context Context of calling activity
* @param drawableId Resource ID of bitmap drawable
* @return Bitmap whose resource id was passed to method.
*/
public static Bitmap getBitmapFromDrawableId(Context context,int drawableId){
Bitmap bitmap = null;
try {
BitmapDrawable drawable = (BitmapDrawable)context.getResources().getDrawable(drawableId);
bitmap = drawable.getBitmap();
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
/**
* This method returns a bitmap related to drawable. It is ready to use method, you can
* use it by simply copying in your project.
*
* @param drawable Drawable resource of image
* @return Bitmap whose resource id was passed to method.
*/
public static Bitmap getBitmapFromDrawable(Drawable drawable){
Bitmap bitmap = null;
try {
BitmapDrawable bitmapDrawable = (BitmapDrawable)drawable;
bitmap = bitmapDrawable.getBitmap();
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
public static Bitmap drawableToBitmap (Drawable drawable) {
ReplyDeleteif (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable)drawable).getBitmap();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}