How to convert a Drawable to a Bitmap?

前端 未结 20 2454
攒了一身酷
攒了一身酷 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);
            }
        }
    }
    

提交回复
热议问题