Picasso ImageError - android

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

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

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?

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
}

Related

Is there a way to get AdMob or AdinCube rewarded video thumbnail?

I want to have a thumbnail in my app to show what ad the user is about to watch before they tap on it. Is there any way I can retrieve an AdMob or AdinCube thumbnail?
Here's how I got it working for RewardedVideoAd. Make sure that you check for null on every step since the thumbnail is not always guaranteed.
if (rewardedVideoAd == null) {
return;
}
if (rewardedVideoAd.isLoaded()) {
Bundle adBundle = rewardedVideoAd.getAdMetadata();
if (adBundle == null) {
return;
}
Object thumbnailUrl = adBundle.get(MEDIA_URL);
if (thumbnailUrl != null) {
Bitmap bitmap = extractImage(thumbnailUrl.toString());
if (bitmap != null) {
// Set the bitmap to your view.
}
}
}

Why do I I get null point exception even though I safeguarded against it?

How do I safeguard against null points? I tried this way:
var spr = context.getSharedPreferences("FAV", Context.MODE_PRIVATE)
var emptyArray: JsonArray = jsonArray()
var myjson = spr.getString("KEY", null)
//GET JSON FROM SHARED PREFERENCE
Log.d("TAG", "NOT NULL")
if(myjson != null) {
var parsedFavFromJson = Gson().fromJson<List<String>>(myjson)
//PARSE FROM JSON TO ARRAY
for (i in parsedFavFromJson) {
emptyArray.add(i)
Log.d("TAG", "" + i)
if (i == recipeArray!![counterX].recipeKey!!) {
Picasso.with(context)
.load(R.drawable.fav_icon_60at3x).into(favMarkerButton)
Log.d("TAG", "Match " + i)
} else {
Log.d("TAG", "WAS NULL")
Picasso.with(context)
.load(R.drawable.fav_unclicked_icon60at3x).into(favMarkerButton)
}
}
}
I have clearly stated the conditional if(myjson != null), still it pass through and crash with the error message java.lang.IllegalStateException: fromJson(json, typeToken<T>()) must not be null. There is no clear indication where the error actually occur - it actually points to an empty code line! If I remove the code above, things are working fine. I also tried using:
var myjson = spr.getString("KEY", null).let {
...and so on, with the same result. Am I missing something here?
UPDATE
I checked variable myjson and it is clearly null. Question is why the conditional if(myjson != null) passed...
In short, you're missing the question mark.
You expect that in block:
spr.getString("KEY", null).let { it }
Won't be null? That's incorrect.
Correct syntax is
spr.getString("KEY", null)?.let { it }
Easy to miss.
Same goes for working with nested JSONs. You should never use bang-bang (!!) in Kotlin. Use the safe syntax instead:
recipeArray?[counterX].recipeKey

NullPointerException in imagepath [duplicate]

This question already has answers here:
Check whether a String is not Null and not Empty
(35 answers)
Closed 6 years ago.
I have a String imagepath which has path of the selected image. If user selects image i have to upload it on sever. I want to check wheter imagepath is null. I have tried this but it give NullPointerException.
This is my code.
public String imagepath = null;
public String imagepath1 = null;
and I am checking if it is null as:
Log.e("imagepath",""+imagepath);
Log.e("imagepath1", ""+imagepath1);
if (imagepath.equals(null)){
Log.e("no image path","dfdsfdsfdsfdsfdf");
}
else {
uploadFile(imagepath);
}
if (imagepath1.equals(null)){
Log.e("no imagepath1 path","imagepath1");
}
else {
uploadFile2(imagepath);
}
If I do not select any image, it shows NullPointerException. Please help me. What is wrong here.
Try this:
if(imagepath != null && imagepath.isEmpty()) { /* do your stuffs here */ }
for more reference check this:
Checking if a string is empty or null in Java
more specific way to check if a string is null or not is using TextUtils:
if (TextUtils.isEmpty(imagepath )) {
Log.d(TAG, "imagepath is empty or null!");
}
You should use try catch to handle this exception because NullPointerException occurs when object not have any value or not defined and here you are not selecting any image that's why NullPointerExcepion occured Reference for nullPointerException
See first that the two strings are not null before proceeding. Try this:
if(imagepath!=null) {
//Do your stuff
uploadfile(imagepath);
} else {
// Handle the exception
}
if(imagepath1!=null) {
//Do your stuff
uploadfile(imagepath1);
} else {
// Handle the exception
}

How to load Facebook image url in volley

Im trying to load Facbeook url image from graph api in volley image loader.
At first there was an error response returning for me 302 and 301 for redirecting the url i check the mirror library for volley and compiled it in the gradle file but now the error response is 400 i don't know what is causing that here is my code :
enter code here
try {
URL picture = new URL("https://graph.facebook.com/v2.4/" + fb_ID + "/picture?type=large);
fb_Picture = picture.toString();
} catch (MalformedURLException e)
{
e.printStackTrace();
}
//Loading the image
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
imageLoader.get("https://graph.facebook.com/v2.4/" + fb_ID + "/picture?type=large", new ImageLoader.ImageListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("ResponseImage", "Image Load Error: " + error.getMessage());
}
#Override
public void onResponse(ImageLoader.ImageContainer response, boolean arg1) {
if (response.getBitmap() != null) {
// load image into imageview
userImage.setOval(true);
userImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
userImage.setImageBitmap(response.getBitmap());
}
}
});
how can i get the actual url image without this url or how can i load this url in volley any help would be appreciated thank you.
If you are doing this in an app, you will need to use an access token to GET https://graph.facebook.com/v2.4/<FBID>/picture?type=large. This will return a JSON object similar to this one:
{
"data": {
"is_silhouette": false,
"url": "https://fb.com/..."
}
}
After parsing, this JSON object's data.url member can be used to create image view objects in your app. You can read the full documentation on this Graph API edge here. You can read more about Using Graph API in general here.
If you are just loading a single profile image uri provided from facebook sdk, you can simply use Asynctask and call BitmapFactory.decodeStream to download the image into an Imageview. The problem with volley is it doesn't handles https images automatically due to shared security reasons and you have to manually create a custom volley Requestque with HurlStack and SSL in order to display the images.

How to upload multiple images on Parse?

I need help with Parse Android API and images uploading/updating. User in my app can create event that has 1 or more images related to that event. So, this images are stored as array object that have parse files.
User can edit images that he added for that event. So, user might want to delete image or to add a new image. So, there I have problem, how I can edit array to delete specific image.
My idea was to download all images on the phone, and when user add/delete image update it locally and then upload again all images to Parse and update that array of images, but it seems that is not working properly, since I get only one image uploaded.
How I can solve this problem, any idea is appreciated.
for (int i = 0; i < ImagesSingleton.getInstance().getBytesList().size(); i++) {
String fileName = FileHelper.getFileName(getActivity(), ImagesSingleton.getInstance().getUrisList().get(i), "image");
byte[] b = ImagesSingleton.getInstance().getBytesList().get(i);
final ParseFile imgFile = new ParseFile(fileName, ImagesSingleton.getInstance().getBytesList().get(i));
imgFile.saveInBackground(new SaveCallback() {
#Override
public void done(com.parse.ParseException e) {
if (e == null) {
listOfFiles.add(imgFile);
if (listOfFiles.size() == ImagesSingleton.getInstance().getUrisList().size()) {
offer.put(ParseConstants.OFFER_PICTURES, listOfFiles);
offer.saveInBackground(new SaveCallback() {
#Override
public void done(com.parse.ParseException e) {
if (e == null) {
mProgressDialog.dismiss();
Toast.makeText(getActivity(), "Sucess saving", Toast.LENGTH_SHORT).show();
ImagesSingleton.getInstance().reset();
transferToRadar();
} else {
mProgressDialog.dismiss();
Toast.makeText(getActivity(), getResources().getString(R.string.error) + e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
});
}
I think it is quite clear in documentation how to delete an element(s) from an Array column in Parse. You simply have to send the list of the files you want to be removed from the Array like this:
offer.removeAll(ParseConstants.OFFER_PICTURES, listOfFilesToRemove);
offer.saveInBackground();

Categories

Resources