Java 在 Android L 中从 ImageView 获取位图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26865787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Bitmap from ImageView in Android L
提问by Utkarsh Srivastav
I want to get Bitmap
from ImageView
. I have used following code, but getDrawable()
returns null. How to get whole Bitmap
from ImageView
.
我想Bitmap
从ImageView
. 我使用了以下代码,但getDrawable()
返回空值。如何Bitmap
从ImageView
.
Bitmap bitmap;
if (mImageViewer.getDrawable() instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) mImageViewer.getDrawable()).getBitmap();
} else {
Drawable d = mImageViewer.getDrawable();
bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
d.draw(canvas);
}
storeImage(bitmap,"final.jpeg");
回答by ToYonos
According to this answer, just do it like this:
根据这个答案,就这样做:
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
回答by droidev
If you just want the Bitmap from a ImageView the following code may work for you:-
如果您只想要 ImageView 中的位图,以下代码可能适合您:-
Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
Try having the image in all drawable qualities folders (drawable-hdpi/drawable-ldpi etc.)
尝试将图像放在所有可绘制质量文件夹(drawable-hdpi/drawable-ldpi 等)中
Could be that the emulator or device your using has a different density and is trying to pull images from another folder.
可能是您使用的模拟器或设备具有不同的密度,并试图从另一个文件夹中提取图像。
If you are using an extension in your image other than .png, .jpg, or .gif, It might not recognize other extension types. http://developer.android.com/guide/topics/resources/drawable-resource.html
如果您在图像中使用了 .png、.jpg 或 .gif 以外的扩展名,它可能无法识别其他扩展名类型。http://developer.android.com/guide/topics/resources/drawable-resource.html
回答by Pankaj Arora
Try this:
尝试这个:
imageView.invalidate(); BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable(); Bitmap bitmap = drawable.getBitmap();
imageView.invalidate(); BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable(); Bitmap bitmap = drawable.getBitmap();
回答by Ness Tyagi
If you are trying to get bitmap from Glide loaded image then this will help you
如果您尝试从 Glide 加载的图像中获取位图,那么这将对您有所帮助
Drawable dr = ((ImageView) imView).getDrawable();
Bitmap bmp = ((GlideBitmapDrawable)dr.getCurrent()).getBitmap();
回答by younes
Take a picture of the ImagView and convert it to a string to send to the server
拍一张 ImagView 的照片,并把它转换成字符串发送到服务器
ImageView ivImage1 = (ImageView ) findViewById(R.id.img_add1_send );
getStringImage( ( ( BitmapDrawable ) ivImage1.getDrawable( ) ).getBitmap( ) ),
public String getStringImage(Bitmap bm){
ByteArrayOutputStream ba=new ByteArrayOutputStream( );
bm.compress( Bitmap.CompressFormat.PNG,90,ba );
byte[] by=ba.toByteArray();
String encod= Base64.encodeToString( by,Base64.DEFAULT );
return encod;
}
回答by Umair Iqbal
If Someone Using Glide Library to load image into ImageView Then Try This One.
如果有人使用 Glide 库将图像加载到 ImageView 中,那么试试这个。
GlideBitmapDrawable drawable = (GlideBitmapDrawable) image.getDrawable();
Bitmap bitmap = drawable.getBitmap();