Zack
Zack

Reputation: 492

Set an image if Uri is empty

I want to set an image to the Uri if the Uri is empty, this is what I did:

if (imageUri == null)
{
    imageUri = imageUri.parse(String.valueOf(R.drawable.no_image_available));
}

It does not give any error but it doesn't work, is there any other way I can use to achieve this?

Upvotes: 1

Views: 4136

Answers (4)

rafsanahmad007
rafsanahmad007

Reputation: 23881

This :

imageUri = imageUri.parse(String.valueOf(R.drawable.no_image_available));

will not give you a valid Uri.

There is Much more easier option:

option 1

 Uri uri = Uri.parse("android.resource://your.package.here/drawable/R.drawable.no_image_available"); 
imageview.setImageURI(uri);  //set the uri

OR use:

if (imageUri == null)
{
    imageView.setImageResource(R.drawable.no_image_available);
}

Upvotes: 0

user7771871
user7771871

Reputation:

Just check that Empty URI is not equal to followUri, this check includes check by null:

 if (!Uri.EMPTY.equals(followUri)) {
        //handle followUri
 }

Upvotes: 0

Vinod Pattanshetti
Vinod Pattanshetti

Reputation: 2583

Instead you can use glide library. If imageUrl is there then it will show otherwise it will take placeHolder image.


    Glide.with(context)
            .load(imageUrl)
            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .dontTransform()
            .placeholder(placeholder)
            .into(imageView);


http://www.gadgetsaint.com/android/circular-images-glide-library-android/#.WNiXPxKGNPN

Upvotes: 1

Bitmap yourImageNotFoundBitmap = BitmapFactory.decodeResource(context.getResources(),
                                           R.drawable.no_image_available);
if (imageUri == null){

yourImage.setImageBitmap(yourImageNotFoundBitmap);

}

Upvotes: 1

Related Questions