gpt4 book ai didi

java - 如何检查 ImageView 是否包含 Bitmap?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:46:11 27 4
gpt4 key购买 nike

我正在实现,如果 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;
}
}

最佳答案

您可以通过以下方式查看:

boolean hasDrawable = (croppedImageView.getDrawable() != null);
if(hasDrawable) {
// imageView has image in it
}
else {
// no image assigned to image view
}

只需检查位图值如下:

if(bitmap == null) {
// set the toast for select image
} else {
uploadImageToServer();
}

关于java - 如何检查 ImageView 是否包含 Bitmap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20324048/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com