Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

java - How to check if ImageView contains Bitmap or not?

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;
    }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...