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.
Related
This question already has answers here:
how to make picasso display default image when there is an invalid path
(3 answers)
How can I use a Color as placeholder image with Picasso?
(3 answers)
Closed 4 years ago.
I'm currently working on RecyclerView with data binding implement, to load Image from a list of url (/images/slider/my/myImage.jpg) get from API.
#BindingAdapter("imageUrl")
public static void loadImage(ImageView imageView, String imageUrl){
Picasso.with(imageView.getContext()).load(CommUtils.WEBSITE_LINK + imageUrl).into(imageView);
}
Currently i have the code above in my ListAdapter. The code able to load the image fine when the url is in correct link or exist in server else it will it as blank. Therefore, i want to create a case that will check if the image is exist/is correct link before display.
What i want to achieve is:
if(Image Link exist){
//Load Image, Picasso.with.................
} else {
//Use Dummy Photo, Picasso.with..................
}
[Edit] So now i know i can use the error() function to create another load if the path is not exist. How about when in a case that my API will return two difference format or "url" which could be; with path (/images/slider/my/myImage.jpg) or without path (myImage.jpg)
Therefore in my code i want to do something like
if(websitelink + ImageUrl){ load image }
else(websitelink + path + ImageUrl) { load iamge} //Should this code run under error() from the first case??
Can i perform a checking on the ImageUrl first instead of try to load the image directly and only change when is error
Picasso supports placeholder and error images anyways:
Picasso.get()
.load(url)
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.user_placeholder_error)
.into(imageView);
So, if all you want to archieve, is to show some error image, when loading does not work, that is all you need.
You can use com.squareup.picasso.Callback to listen for response.:
Picasso.with(getContext())
.load(url)
.placeholder(R.drawable.image_name) // Your dummy image...
.into(imageView, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
// Image is loaded successfully...
}
#Override
public void onError() {
// Unable to load image, may be due to incorrect URL, no network...
}
});
I load image from URL using picasso first time on line, After then use from cache. Any URL from web is load in imageview on line or off line. But my server image URL is load image in on line not in off line. I use below code from image load.
Picasso.with(mContext)
.load(urlProfile)
.networkPolicy(NetworkPolicy.OFFLINE)
.placeholder(R.drawable.ic_place_holder)
.into(imageView, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Picasso.with(mContext)
.load(urlProfile)
.placeholder(R.drawable.ic_place_holder)
.into(imageView);
}
});
Web url load in online or offline both : URL
My server url load image only in online: URL
I show in cache directory and found that image of my server URL is not cached. Any have idea about that.
Hi below is my solutions and it's working perfectlly.
Picasso.with(mContext)
.load(Uri.parse(urlProfile))
.networkPolicy(NetworkPolicy.OFFLINE)
.into(iv_view, new Callback() {
#Override
public void onSuccess() {
// if you are showing progress then handle it on here
}
#Override
public void onError() {
// Try again online if cache failed and download using internet
new Picasso.Builder(mContext)
.downloader(new OkHttpDownloader(mContext, Integer.MAX_VALUE))
.build()
.load(Uri.parse(urlProfile))
.placeholder(R.mipmap.ic_launcher)
.into(iv_view);
}
});
Hope this helps you..
By the way this is very old but you can use Glide for better performance.
I have a problem that each time , I take a photo and and I try to display it. Glide load me the old file. I check the file locally , it has been changed successfully. But the old image is displayed.
This is my code :
public void onCameraCaptureCompleted(final File photoFile) {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
stopCamera();
pickedImg.setImageBitmap(null);
Glide.with(TakePhotoFragment.this)
.load(photoFile)
.asBitmap()
.dontAnimate()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(pickedImg);
}
});
}
Who knows how to solve this problem ?
Try with skipMemoryCache
Allows the loaded resource to skip the memory cache.
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
NOTE
If same problem still coming then use clearMemory().
For good approach, Create Application Class and add this
public class RootApplication extends Application
{
#Override public void onLowMemory() {
super.onLowMemory();
Glide.get(this).clearMemory();
}
Make sure, added this Application class in Manifest.
<application
android:name=".RootApplication"
>
you may want to clear the cache stored and then restart the application manually
or
just clear the cache from code only. just call clearMemory() on glide.
Have a look here for more info
Docs
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
just this worked for me.
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);
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);