How to convert a Drawable to a Bitmap?

前端 未结 20 2478
攒了一身酷
攒了一身酷 2020-11-21 22:46

I would like to set a certain Drawable as the device\'s wallpaper, but all wallpaper functions accept Bitmaps only. I cannot use WallpaperMan

相关标签:
20条回答
  • 2020-11-21 23:06
     // get image path from gallery
    protected void onActivityResult(int requestCode, int resultcode, Intent intent) {
        super.onActivityResult(requestCode, resultcode, intent);
    
        if (requestCode == 1) {
            if (intent != null && resultcode == RESULT_OK) {             
                Uri selectedImage = intent.getData();
    
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                filePath = cursor.getString(columnIndex);
    
                //display image using BitmapFactory
    
                cursor.close(); bmp = BitmapFactory.decodeFile(filepath); 
                iv.setBackgroundResource(0);
                iv.setImageBitmap(bmp);
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-21 23:08

    Here is the nice Kotlin version of the answer provided by @Chris.Jenkins here: https://stackoverflow.com/a/27543712/1016462

    fun Drawable.toBitmap(): Bitmap {
      if (this is BitmapDrawable) {
        return bitmap
      }
    
      val width = if (bounds.isEmpty) intrinsicWidth else bounds.width()
      val height = if (bounds.isEmpty) intrinsicHeight else bounds.height()
    
      return Bitmap.createBitmap(width.nonZero(), height.nonZero(), Bitmap.Config.ARGB_8888).also {
        val canvas = Canvas(it)
        setBounds(0, 0, canvas.width, canvas.height)
        draw(canvas)
      }
    }
    
    private fun Int.nonZero() = if (this <= 0) 1 else this
    
    0 讨论(0)
  • 2020-11-21 23:09

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

    (Updated to handle a quick conversion for BitmapDrawables and to ensure that the Bitmap created has a valid size)

    public static Bitmap drawableToBitmap (Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable)drawable).getBitmap();
        }
    
        int width = drawable.getIntrinsicWidth();
        width = width > 0 ? width : 1;
        int height = drawable.getIntrinsicHeight();
        height = height > 0 ? height : 1;
    
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap); 
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
    
        return bitmap;
    }
    
    0 讨论(0)
  • 2020-11-21 23:10

    This piece of code helps.

    Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                                               R.drawable.icon_resource);
    

    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);
    }
    
    0 讨论(0)
  • 2020-11-21 23:10

    android-ktx has Drawable.toBitmap method: https://android.github.io/android-ktx/core-ktx/androidx.graphics.drawable/android.graphics.drawable.-drawable/to-bitmap.html

    From Kotlin

    val bitmap = myDrawable.toBitmap()
    
    0 讨论(0)
  • 2020-11-21 23:10

    Here is better resolution

    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;
    }
    
    public static InputStream bitmapToInputStream(Bitmap bitmap) {
        int size = bitmap.getHeight() * bitmap.getRowBytes();
        ByteBuffer buffer = ByteBuffer.allocate(size);
        bitmap.copyPixelsToBuffer(buffer);
        return new ByteArrayInputStream(buffer.array());
    }
    

    Code from How to read drawable bits as InputStream

    0 讨论(0)
提交回复
热议问题