how to check if an ImageView is attached with image in android

后端 未结 7 1474
半阙折子戏
半阙折子戏 2020-12-13 05:43

I am setting an image to ImageView in android code not in xml, but could not make out how to check whether that image has been set in or not in java.

Tried with

相关标签:
7条回答
  • 2020-12-13 06:13
    ImageView myImage = (ImageView) findViewById(R.id.imageView);
    
    if (myImage.getDrawable() == null){
    
    //The imageView is empty
    
    } else{ 
    
    
    // The imageView is occupied.
    
    }
    

    or

    ImageView myImage = (ImageView) findViewById(R.id.imageView);
    
    if ( null == myImage.getDrawable()){
    
    //The imageView is empty
    
    } else{ 
    
    
    // The imageView is occupied.
    
    }
    
    0 讨论(0)
  • 2020-12-13 06:14

    imageViewOne.getVisibility() == 0

    use this instead:

    imageViewOne.getDrawable() == null
    
    0 讨论(0)
  • 2020-12-13 06:18
    if (img_like.getTag() != null && img_like.getTag().toString().equals("red")) {
        img_like.setImageResource(R.drawable.heart);
        img_like.setTag("heart");
    } else {
        img_like.setImageResource(R.drawable.red);
        img_like.setTag("red");
    }
    
    0 讨论(0)
  • 2020-12-13 06:25

    Note that if you set an image via ImageView.setImageBitmap(BITMAP)it internally creates a new BitmapDrawableeven if you pass null. In that case the check imageViewOne.getDrawable() == nullis false anytime. To get to know if an image is set you can do the following:

    private boolean hasImage(@NonNull ImageView view) {
         Drawable drawable = view.getDrawable();
         boolean hasImage = (drawable != null);
    
         if (hasImage && (drawable instanceof BitmapDrawable)) {
             hasImage = ((BitmapDrawable)drawable).getBitmap() != null;
         }
    
         return hasImage;
    }
    
    0 讨论(0)
  • 2020-12-13 06:32

    You can do imageViewOne.getDrawable() for the image you set on the src attribute - meaning setImageResource/Bitmap. Or imageViewOne.getBackground() for the background attribute - meaning setBackground.

    0 讨论(0)
  • 2020-12-13 06:33

    The correct way to check if the ImageView is attached with the image is:

     if (imageView.getDrawable() == null){
           //Image doesn´t exist.
       }else{
            //Image Exists!.
     }
    

    Some methods to load images into the ImageView like using Glide or Picasso have a little delay so we must wait for some milliseconds to check:

        //Load Image.
        Glide.with(this)
                .load(imageURL)
                .into(imageView);
    
       //Wait for 500 ms then check!.
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
               if (imageView.getDrawable() == null){
                  //Image doesn´t exist.
               }else{
                  //Image Exists!.
               }
            }
        }, 500
    
    0 讨论(0)
提交回复
热议问题