4

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:_

 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;
    }
    }
Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
amita
  • 155
  • 2
  • 3
  • 10

2 Answers2

13

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();
}
Johan
  • 8,068
  • 1
  • 33
  • 46
Amulya Khare
  • 7,718
  • 2
  • 23
  • 38
  • boolean hasDrawable = (croppedImageView.getDrawable() != null); if(hasDrawable) { System.out.println("drawableeeeee "+hasDrawable); } else { System.out.println("drawableeeeee "+hasDrawable); } i implemented this code to check out but it also not giving any response. – amita Dec 02 '13 at 08:52
  • what do you mean by _"not giving any response"_? Do you see other output from `System.out.println`? See http://stackoverflow.com/q/2220547/827110 – Amulya Khare Dec 02 '13 at 08:57
  • No its not printing anything . – amita Dec 02 '13 at 09:09
  • Please use `Log.d` to log output to `logcat` or use a debugger to verify. Also click on the link I have posted in the comment and read it. – Amulya Khare Dec 02 '13 at 09:12
1

The accepted answer is not correct for at least one case: when you previously set ImageViews Bitmap to 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.

You can easily find out what's going on in the ImageView source code:

 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.

Therefore, the correct method of checking whether an ImageView has somewhat meaningful Drawable is something like:

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.

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129