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
71
// 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);
}
}
}
671
这会将BitmapDrawable转换为Bitmap .
Drawable d = ImagesArrayList.get(0);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
// We ask for the bounds if they have been set as they would be most
// correct, then we check we are > 0
final int width = !drawable.getBounds().isEmpty() ?
drawable.getBounds().width() : drawable.getIntrinsicWidth();
final int height = !drawable.getBounds().isEmpty() ?
drawable.getBounds().height() : drawable.getIntrinsicHeight();
// Now we check we are > 0
final Bitmap bitmap = Bitmap.createBitmap(width <= 0 ? 1 : width, height <= 0 ? 1 : height,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
15 回答
使用此代码:
这段代码有帮助 .
这是一个下载图像的版本 .
Drawable
可以绘制到Canvas
上,Canvas
可以由Bitmap
支持:(已更新以处理
BitmapDrawable
的快速转换并确保创建的Bitmap
具有有效大小)这是更好的分辨率
代码来自How to read drawable bits as InputStream
这是@ Chris.Jenkins提供的答案的好Kotlin版本:https://stackoverflow.com/a/27543712/1016462
这会将BitmapDrawable转换为Bitmap .
METHOD 1 :您可以直接转换为这样的位图
METHOD 2 :您甚至可以将资源转换为drawable,从中可以获得这样的位图
对于API> 22
getDrawable
方法移动到ResourcesCompat
类,所以你做这样的事情Android提供了一个非直接的解决方案:
BitmapDrawable
. 要获取位图,我们必须将资源IDR.drawable.flower_pic
提供给aBitmapDrawable
,然后将其转换为Bitmap
.ImageWorker Library可以将位图转换为drawable或base64,反之亦然 .
履行
在项目级别Gradle中
在应用程序级别Gradle中
您还可以从外部存储和检索位图/ drawables / base64图像 .
点击这里https://github.com/1AboveAll/ImageWorker/edit/master/README.md
非常简单
使用此代码 . 它将帮助您实现目标 .
也许这会帮助别人......
从PictureDrawable到Bitmap,使用:
......如此实施:
所以在查看(和使用)其他答案之后,似乎他们都严重处理
ColorDrawable
和PaintDrawable
. (特别是在棒棒糖上)似乎调整了Shader
s,因此没有正确处理固体颜色块 .我现在使用以下代码:
与其他人不同,如果在要求将其转换为位图之前在
Drawable
上调用setBounds
,它将以正确的大小绘制位图!