-1

I am using Picasso to show images from server in my android App.I have 5 image URL (HTTP form) getting from server and storing it in a String value.If i send a Correct link to Picasso (.jpg form) it Runs correctly and show my image in my imageview and if send a wrong link in (.pdf form) it shows error in my Image View,But when ever i pass null value or empty value from my server to string my app crash its running if statement first even if its value is null or empty else statement is not running what should i update in my code so that if i get null value from server my imageview should show and error and text view value should be changed.

// Code only where my If/Else Start :

    if (image_fourth != null && image_fourth != ""){
        Picasso.get().load(image_fourth).resize(200, 200).placeholder(R.drawable.placeholder).error(R.drawable.error).into(image1);
        image1.setVisibility(View.VISIBLE);

        buttons.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (image_second == null){
                    image_2t.setText("Image Not Found");
                    image_2t.setVisibility(View.GONE);


                }
                else if (image_second != null){
                    Picasso.get().load(image_second).resize(200, 200).placeholder(R.drawable.placeholder).error(R.drawable.error).into(image2);
                    image2.setVisibility(View.VISIBLE);
                    image_2t.setText("Image 2");
                    image_2t.setVisibility(View.VISIBLE);
                }
            }
        });
    }
    else{
        image_1t.setText("Image Not Found");
    }
Sunny
  • 79
  • 10
  • what type of error comes? – Bharat Vankar Jan 09 '20 at 04:31
  • @BharatVankar if my string is null its point error to line " Picasso.get().load(image_fourth).resize(200, 200).placeholder(R.drawable.placeholder).error(R.drawable.error).into(image1); " – Sunny Jan 09 '20 at 04:38

3 Answers3

3

Use Glide instead of Picasso, then you don't need to add any other conditions or code to check if the string is empty or not, Check this sample

Glide.with(activity_context)
                .load(your_url)
                .placeholder(R.drawable.default_image)
                .error(R.drawable.default_image)
                .override(200, 200)
                .centerCrop()
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(mHolder.imgIcon); 

you can find more about Glide here

if you want to do continue with Picasso, try this code, this may help you

if(!TextUtils.isEmpty(Url)) {

            Picasso.with(activity).load(Url.replace(" ", "%20")).error(R.drawable.default_image).networkPolicy(NetworkPolicy.NO_CACHE)
                    .memoryPolicy(MemoryPolicy.NO_CACHE)

                    .into(imageView, new Callback() {

                        public void onSuccess() {
                            System.out.println(" 02062015:onSuccess");


                        }

                        @Override
                        public void onError() {
                            imageView.setImageResource(R.drawable.default_image);
                            System.out.println(" 02062015:onError");

                        }
                    });
        }
Sharon Joshi
  • 498
  • 7
  • 19
2

You could try reversing the if/else statement. Picasso, as far as I know, cannot take a null or empty string in load(). In your case the if statement could cover if the string/url source is null you could load a placeholder:

    if (url == null || url.isEmpty()) {
    Picasso.with(context).load(placeholder).transform(transformation).into(this)
} else {
    Picasso.with(context).load(url).error(placeholder).transform(transformation)
            .placeholder(placeholder).into(this)
}

Another option might be to keep your if statement as it is but add this to the else statement:

else
        image_fourth.setImageResources(R.mipmap.ic_launcher);

I have used similar techniques with Glide. That worked for me. For more info you can check this stackoverflow answer: Picasso doesn't tolerate empty String URL?

Adam391
  • 105
  • 2
  • 6
  • sir thnku for your help can uh help me out with this new problem i am faccing ?? LINK to Question: https://stackoverflow.com/questions/59713938/array-list-to-server-using-volley – Sunny Jan 13 '20 at 10:13
  • I don't know a lot about Volley unfortunately so I am not sure if I would be able to help you with that next one – Adam391 Jan 13 '20 at 15:57
0

Just switch between statements and check is it working?

if (image_fourth == null || image_fourth == ""){
//write your else statement here
}else{
//And if code is here
}
Deva GAikwad
  • 76
  • 1
  • 8