Tuesday, June 5, 2012

How to convert a Drawable to a Bitmap?


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

6 comments:

  1. This converts a Drawable to a Bitmap.

    Drawable d = ImagesArrayList.get(0);
    Bitmap bitmap = ((BitmapDrawable)d).getBitmap();

    ReplyDelete
  2. This piece of code helps.

    Bitmap 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);
    }

    ReplyDelete
  3. A Drawable can be drawn onto a Canvas, and a Canvas can be backed by a Bitmap:

    Bitmap 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);

    ReplyDelete
  4. Maybe this will help someone...

    From 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
  5. /**
    * 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;
    }

    ReplyDelete
  6. public static Bitmap drawableToBitmap (Drawable drawable) {
    if (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;
    }

    ReplyDelete