Java 如何检查 ImageView 是否包含 Bitmap?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20324048/
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
How to check if ImageView contains Bitmap or not?
提问by amita
I am implementing if a ImageView
has bitmap then it should save the image from imageview to internal memory ,otherwise set another bitmap in internal memory of application.
here is code:_
我正在实现如果一个ImageView
位图那么它应该将图像从 imageview 保存到内部存储器,否则在应用程序的内部存储器中设置另一个位图。这是代码:_
croppedImage = cropImageView.getCroppedImage();
croppedImageView = (ImageView) findViewById(R.id.croppedImageView);
croppedImageView.setImageBitmap(croppedImage);@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_save:
counter++;
if(croppedImageView.getDrawable() != null)
{
System.out.println("nullllllllllllll");
try {
Bitmap photo = ((BitmapDrawable)croppedImageView.getDrawable()).getBitmap();
FileOutputStream mFileOutStream1 = openFileOutput("IMG" + counter + ".png", Context.MODE_PRIVATE);
photo.compress(CompressFormat.JPEG, 100, mFileOutStream1);}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();}
}else{
System.out.println("notttttnullllllllllllll");
try {
FileOutputStream mFileOutStream1 = openFileOutput("IMG" + counter + ".png", Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, mFileOutStream1);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Editor editor = def.edit();
editor.putInt("value", counter);
editor.commit();
break;
default:
break;
}
}
采纳答案by Amulya Khare
You can check it as follows:
您可以按如下方式检查:
boolean hasDrawable = (croppedImageView.getDrawable() != null);
if(hasDrawable) {
// imageView has image in it
}
else {
// no image assigned to image view
}
Just check only Bitmap value as below :
只需检查如下位图值:
if(bitmap == null) {
// set the toast for select image
} else {
uploadImageToServer();
}
回答by Alexander Abakumov
The accepted answeris not correctfor at least one case: when you previously set ImageView
s Bitmap
to null
via:
至少在一种情况下,接受的答案不正确:当您之前将ImageView
s设置Bitmap
为null
via 时:
imageView.setImageBitmap(null);
actually it would NOT set the internal Drawable
to null
. So, the proposed in the accepted answer check will give you incorrect result.
实际上它不会将内部设置Drawable
为null
. 因此,接受的答案检查中的建议会给您不正确的结果。
You can easily find out what's going on in the ImageView
source code:
您可以轻松找出ImageView
源代码中发生的事情:
public void setImageBitmap(Bitmap bm) {
// if this is used frequently, may handle bitmaps explicitly
// to reduce the intermediate drawable object
setImageDrawable(new BitmapDrawable(mContext.getResources(), bm));
}
Meaning that instead of setting its internal Drawable
to null
, it sets it to a newly created BitmapDrawable
with null
Bitmap
.
这意味着不是将其内部设置Drawable
为null
,而是将其设置为新创建BitmapDrawable
的null
Bitmap
.
Therefore, the correct method of checking whether an ImageView
has somewhat meaningful Drawable
issomething like:
因此,检查 an 是否ImageView
具有某种意义的正确方法Drawable
是:
publie static boolean hasNullOrEmptyDrawable(ImageView iv)
{
Drawable drawable = iv.getDrawable();
BitmapDrawable bitmapDrawable = drawable instanceof BitmapDrawable ? (BitmapDrawable)drawable : null;
return bitmapDrawable == null || bitmapDrawable.getBitmap() == null;
}
Moreover, looking at this behavior in the source code, one might suppose that null
Drawble
is something Android SDK developers try to avoid. That's why you should avoid relying on getDrawable() == null
check in your code at all.
此外,查看源代码中的这种行为,人们可能会认为这null
Drawble
是 Android SDK 开发人员试图避免的事情。这就是为什么您应该完全避免依赖getDrawable() == null
检查代码的原因。