如何检查ImageView是否包含位图?

时间:2013-12-02 08:44:06

标签: java android android-camera android-imageview drawable

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

2 个答案:

答案 0 :(得分:12)

您可以按如下方式查看:

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

只检查Bitmap值,如下所示:

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

答案 1 :(得分:0)

至少有一种情况accepted answer 不正确:之前您通过以下方式将ImageView Bitmap null设置为imageView.setImageBitmap(null);

Drawable

实际上它不会将内部null设置为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)); } source code中发生的事情:

Drawable

这意味着它不是将其内部null设置为BitmapDrawable,而是将其设置为新创建的null Bitmap 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; } 的正确方法类似于:

null

此外,在源代码中查看此行为时,可能会认为Drawble getDrawable() == null是Android SDK开发人员试图避免的。这就是为什么你应该完全依赖body { background-color: #EEE; } .container { display: flex; flex-flow: row wrap; } .card { width: 20em; height: 20em; padding: 2em; background-color: white; margin: 2em; box-shadow: 0 0 5px #222; } .pie-center { background: none; border-radius: 50%; transform: rotate(-90deg); } .circle1 { fill: none; stroke: teal; stroke-width: 7; stroke-dasharray: 30 70; } .tooltiptext { font-size: 5px; background-color: black; color: #fff; text-align: center; padding: 2px 0; border-radius: 6px; }检查代码的原因。