Unable to download image using Picasso on Android - android

I'm trying to download the following image using Picasso https://s3-media4.fl.yelpcdn.com/bphoto/94E7Ti0RTDbA6mGotZw5DA/o.jpg
I can see it fine in a browser. However when I attempt to download it using Picasso, I get an error (a breakpoint in my onError() method gets hit).
This is an extract of my code:
final RequestCreator rc = with(context).load(fullImagePath);
if (fit != null && fit) {
rc.fit();
}
// If no callback listener exists, create one.
if (callbackListener == null) {
callbackListener = new Callback() {
#Override
public void onSuccess() {
L.p("onSuccess retrieving " + fullImagePath);
}
#Override
public void onError() {
// Something went wrong
L.p("Error retrieving " + fullImagePath);
}
};
}
rc.into(fImageView, callbackListener);
I found this: https://github.com/square/picasso/issues/500 however it's a bit dated and the OkHttpClient class no longer has the setProtocols() method.
Thanks!

Inserting that link as "http" instead of "https" will work.

The below code works for me for both http and https links.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
String https = "https://s3-media4.fl.yelpcdn.com/bphoto/94E7Ti0RTDbA6mGotZw5DA/o.jpg";
String http = "http://www.sunseed.org.uk/projectpack/files/2014/05/Library_1400_800-1200x686.jpg";
Picasso.with(this)
.load(https)
.placeholder(R.drawable.me)
.into(imageView);
}
Also be sure you write the below permission in manifest file:
<uses-permission android:name="android.permission.INTERNET" />
also write the following in build.gradle file
implementation 'com.squareup.picasso:picasso:2.5.2'

Related

Image not loading through URL in ImageView using Picasso android

I am using the Picasso library to set the image from URL. This URL is working in other programming language but not in Android.
Picasso.with(context).load(product_modal.getImage()).placeholder(R.drawable.ic_no_image).into(holder.iv_thumbnail_filled);
Finally, I found the actual problem that you have faced. Replace https to http in your URL. Because your site does not have SSL.
Just created a method for loading image
private void loadImage(final ImageView imageView, final String imageUrl){
Picasso.get()
.load(imageUrl)
.placeholder(R.drawable.image_white)
.into(imageView , new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
String updatedImageUrl;
if (imageUrl.contains("https")){
updatedImageUrl = imageUrl.replace("https", "http");
}else{
updatedImageUrl = imageUrl.replace("http", "https");
}
loadImage(imageView, updatedImageUrl);
}
});
}
You just need to provide an imageView and the image URL. For the first time if image not loaded then its try to replace https to http and then try load the image.
Using the method by using this:
loadImage(holder.iv_thumbnail_filled, product_modal.getImage());
And make sure you have Internet Access Permission on AndroidMenifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
Hope this will solve your problem.
According to Picasso library, they've changed the way of loading images
Please use the below to load image
Picasso.get().load(product_modal.getImage()).placeholder(R.drawable.ic_no_image).into(holder.iv_thumbnail_filled);
You can check this question for more info.
If you have HTTP 504 error, have a try:
Uninstalling the app and installing it again!
Info from here.

Load Image one time with Picasso library

I am using Picasso library to get Image from an url.
My problem is when I load an Image for the first time and I exit out of my app and after I come back my app tries to load the Image again but I don't want it happen. Are there any other way to do that(load just one time the Image and in others time don't need to Internet for load)?
The issue with the above answers is that they only check the availability of the images in the disk cache, it does not cover the part if the image does not exist in the cache to go online and retrieve it.
First make a class that extends Application (You can name it whatever you want that does not interfere with your application, my convention is to use "Global").
public class Global extends Application {
#Override
public void onCreate() {
super.onCreate();
Picasso.Builder builder = new Picasso.Builder(this);
builder.downloader(new OkHttpDownloader(this,Integer.MAX_VALUE));
Picasso built = builder.build();
built.setIndicatorsEnabled(false);
built.setLoggingEnabled(true);
Picasso.setSingletonInstance(built);
}
}
Make sure you add dependancy for OkHttp library, it's developed by the same guys from Picasso
compile 'com.squareup.okhttp:okhttp:2.4.0'
and add the class in your Manifest file Applications tag :
android:name=".Global"
Then when you want to retrieve the image :
Picasso.with(context)
.load(Image URL)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(imageView, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
//Try again online if cache failed
Picasso.with(context)
.load(Image URL)
.into(imageView, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Log.v("Picasso","Could not fetch image");
}
});
}
});
The above method checks if the image is already cached, if not gets it from the internet.
try this:
Picasso.with(this).load(url).networkPolicy(NetworkPolicy.OFFLINE).into(imageView);
try this:
Picasso.with(this).invalidate(url);
Picasso.with(this)
.load(url)
.networkPolicy(
NetworkUtils.isConnected(this) ?
NetworkPolicy.NO_CACHE : NetworkPolicy.OFFLINE)
.resize(200, 200)
.centerCrop()
.placeholder(R.mipmap.ic_avatar)
.error(R.mipmap.ic_avatar)
.into(imageView);

URL image will not set to ImageView Android

Im trying to load an image from url to my android ImageView. but it gives no image for my url. but when i call another sample url it loads on the ImageView
My URL which gives empty
https://192.168.100.15/HeyVoteWeb/Home/GetImage/d9cbd32c-47fc-4644-ab97-1f525c96e9ed/100000102
This sample URL works for me
https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png
This is the code i am working on
public class GetImage extends Activity{
ImageView postpic1;
Bitmap b;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_posts);
postpic1 = (ImageView) findViewById(R.id.postpic1);
information info = new information();
info.execute("");
}
public class information extends AsyncTask<String, String, String>
{
#Override
protected String doInBackground(String... arg0) {
try
{
URL url = new URL("https://localhost/HeyVoteWeb/Home/GetImage/d9cbd32c-47fc-4644-ab97-1f525c96e9ed/100000102");
InputStream is = new BufferedInputStream(url.openStream());
b = BitmapFactory.decodeStream(is);
} catch(Exception e){}
return null;
}
#Override
protected void onPostExecute(String result) {
postpic1.setImageBitmap(b);
}
}
}
The URL for your image is localhost. Localhost(127.0.0.1) refers to same machine as the request origination. So, your phone sends request to itself. Instead specify the IP address of your pc where the server is running.
PS: Ensure both your PC and your phone are connected to the same network.
I think that your problem is in your URL, replace your localhost with your IP address, hope it solves your problem.
Just use image loading and caching libraries. For instance Picasso
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Alternative solution is Glide; It has a similar working principleIt has a similar working principle:
Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView);
Do you have a local webserver running which supports HTTPS? Because that is where you are trying to load an image from.
Also, if you have on running, do you get the image when you call your desired URL in your browser?
Have you tried using Picasso library very easy and effective:
Go to you build.gradle inside app dir and add to dependencies:
compile 'com.squareup.picasso:picasso:2.5.2'
Then use picasso lib:
String url = "https://localhost/HeyVoteWeb/Home/GetImage/d9cbd32c-47fc-4644-ab97-1f525c96e9ed/100000102";
Picasso.with(context) //The context of your activity
.load(url)
.into(postpic1);

Picasso library does not load images from SD card on Android

I take a file from path from image gallery and try to load it a image view as follows. File path is: /storage/sdcard0/DCIM/Camera/1436267579864.jpg. I also tried passing Uri I also have read privileges to SD card.
It ends up in onError() method. However similar method works fine for web urls. How can I resolve this?
private void getImage(File file) {
if(file.exists()) {
Picasso.with(activity)
.load(file)
.error(R.drawable.noimage)
.into(imgPreview, new Callback() {
#Override
public void onSuccess() {
if (progressBar != null && imgPreview != null) {
imgPreview.setVisibility(View.VISIBLE);
imgPreview.setTag("loaded");
progressBar.setVisibility(View.GONE);
}
}
#Override
public void onError() {
if (progressBar != null && imgPreview != null) {
imgPreview.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
}
}
});
}
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Though it is too late , but I stuck into same problem, so I solved in the following way . Just using and appending "file://" in the starting of path. take a look at this:
Picasso.with(context) //
.load("file://" +myFilePath) //
.error(R.mipmap.error)
.placeholder(R.mipmap.ic_launcher)
.fit()
.tag(MyActivity.this) //
.into(imageView, new ImageLoadedCallback(progressBar) {
#Override
public void onSuccess() {
progressBar.setVisibility(View.GONE);
}
#Override
public void onError() {
Log.d("Picasso Error", "Error");
}
});
This solves my problem . Just answering so that if some one fall in the same problem and came here for solution then he may get his problem solved through this.

Picasso Library, Android: Using Error Listener

I'm playing around with the Picasso library for image loading, but I'm running into an issue. When an image fails to load, I want to hide the view rather than load in a default image. I noticed from the source that it looks like the only way to add a listener is from the builder, but the error method is never called when an image does fail to load. Anyone have any experience with this?
iv = (ImageView) findViewById(R.id.imageView);
Picasso.Builder builder = new Picasso.Builder(getApplicationContext());
builder.listener(new Picasso.Listener() {
#Override
public void onImageLoadFailed(Picasso arg0, String arg1) {
Log.e("Picasso Error", "Errored out, hiding view");
iv.setVisibility(View.GONE);
}
});
Picasso pic = builder.build();
pic.load("thisshouldbreak.jpg").into(iv);
Picasso 2.0 allows you to attach a callback into a request.
https://github.com/square/picasso
The callback you are using is for "global" listener and it helps you debug errors that potentially happen due to a network load.
Use load(url).into(view, new Callback() {...}); in Picasso 2.0.
Remember to invoke cancelRequest(target) if you are using a Callback.
My example:
Picasso picasso = new Picasso.Builder(parent.getContext())
.listener(new Picasso.Listener() {
#Override
public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
//Here your log
}
})
.build();
picasso.load(shopModel.getShopImg())
.fit()
.into(viewHolder.shopImg);
You can try to add a 'global' listener.
// create Picasso.Builder object
Picasso.Builder picassoBuilder = new Picasso.Builder(this);
picassoBuilder.listener(new Picasso.Listener() {
#Override
public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
Log.e("PICASSO", uri.toString(), exception);
}
});
// Picasso.Builder creates the Picasso object to do the actual requests
Picasso picasso = picassoBuilder.build();
try {
Picasso.setSingletonInstance(picasso);
} catch (IllegalStateException ignored) {
// Picasso instance was already set
// cannot set it after Picasso.with(Context) was already in use
}
Any subsequent calls to Picasso.with(Context context) will return the instance which connected to listener, so all fails will be logged.
Please note that you need to call setSingletonInstance as soon as possible, e.g. in Application onCreate.
P.S. Code adopted from here - Customizing Picasso with Picasso.Builder
My answer:
File file = new File(filePath);
Picasso.with(context).load(file).placeholder(R.drawable.draw_detailed_view_display).error(R.drawable.draw_detailed_view_display)
.resize(400, 400).into(mImageView, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
mImageView.setVisibility(View.GONE);
}
});
When we got error, error goes to onError method then we handle it!
private void getAvatar(){
Picasso.with(this)
.load(Links.GET_AVATAR + ".jpg")
.into(imgUserAvatar, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
imgUserAvatar.setImageResource(R.drawable.icon_profile_default);
}
});
}
Just a suggestion, but you might avoid issues in programming if you make an "empty" png file and set it as the default image file in your res folder... kinda silly i know... but likely to work without fighting...

Categories

Resources