Using placeholder in Coil when the url is null - android

I get some data from database and push it to reciclerview. I use Coil to show images. But there are some urls that are null and I need to use some placeholder in such cases.
I tried all the option I saw in the internet but nothing helped.
binding.posterIv.load(UtilsUi.BASE_URL + recyclerViewItem.poster_path){
placeholder(R.drawable.placeholder_male)
error(R.drawable.placeholder_male)
fallback(R.drawable.placeholder_male)
}
When the url is null I just get empty black space.
ImageView looks like this:
<ImageView
android:id="#+id/actorImageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/keanuriives" />
Do you have any idea how to fix it?
P.S. Coil needs to be used, not Glide or Picasso.

Related

How to load Image and other content in UI from server

Taking example of viewpager or recyclerview in android we know that they can contain images as well.What i want to know is that how we can load images from database from server and put that images in view pager or recycler view.Upto know my understanding is that i have to make a database in server and put images in it ,now during splash screen download that images and after that put that images into view.Am i going right? Basically what i want to achieve is like shopping app which shows images which changes time to time ? how i can do same ?Do i should go with Rest API to download that images from server and then put in view also do i need to make sure that every time user opens app then it download image?
You can simply fetch .jpg URL from API and then load that image in your app by using Fresco library.
Check this out: Display images with Fresco
First add Fresco to your dependencies in app/build.gradle.
dependencies {
implementation 'com.facebook.fresco:fresco:1.10.0'
}
Don't forget to add in your AndroidManifest.xml proper permission:
<uses-permission android:name="android.permission.INTERNET"/>
Initialize Fresco in class that extend Application class. You can do it in Activity, but it's better to do it just once in Application class.
Fresco.initialize(context);
Instead of ImageView use SimpleDraweeView like this:
<com.facebook.drawee.view.SimpleDraweeView
android:id="#+id/sdvImage"
android:layout_width="130dp"
android:layout_height="130dp"
fresco:placeholderImage="#drawable/myPlaceholderImage" />
Then just init your SimpleDraweeView object in Activity/ViewHolder/Fragment, parse Url string to Uri like this:
Uri imageUri = Uri.parse("https://i.imgur.com/tGbaZCY.jpg");
And you can set Uri to your SimpelDraweeView object this way:
draweeView.setImageURI(imageUri);
You can use Glide or Picasso as well. Find one that suit your needs by read this post:
Picasso v/s Imageloader v/s Fresco vs Glide

How to set image to Google Play Books API thumbnail using Fresco?

I am trying to load in a Google Play books image thumbnail from a URL using Fresco (I've also tried using Glide and Picasso) 1.10.0. I am able to load in another image (such as this https://i.imgur.com/6zDqjm8.jpg), but only the placeholder image shows when I use the Google Play Books thumbnail link (e.g. http://books.google.com/books/content?id=F1wgqlNi8AMC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api).
I've also tried adding ".jpeg" on to the end of the link (since in a web browser the image still shows) in the Uri.parse code, but it doesn't make a difference.
Do you know why this is and/or what I am doing wrong?
I have got the INTERNET permission in my manifest. I also have the Fresco namespace in my XML file.
My Fresco code (taken from Getting Started with Fresco guide):
Uri uri = Uri.parse("http://books.google.com/books/content?id=F1wgqlNi8AMC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api");
SimpleDraweeView draweeView = findViewById(R.id.my_image_view);
draweeView.setImageURI(uri);
XML code for the SimpleDraweeView:
<com.facebook.drawee.view.SimpleDraweeView
android:id="#+id/my_image_view"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
fresco:placeholderImage="#drawable/search_white_24dp" />
JSON I'm working with (for more Google Play Books thumbnails): https://pastebin.com/hq4AFMRb
Please let me know if you need any more information (e.g. XML). I don't mind using a different third-party library (including Glide or Picasso) if that works.
Looks like, that unprotected traffic is no longer available for making requests to the Google Books servers (Pay attention at the piece of the Glide's log below):
08-09 22:31:06.428 7606-7606/com.sandbox.test W/Glide: Load failed for
http://books.google.com/books/content?id=F1wgqlNi8AMC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api
with size [2392x2392]
class com.bumptech.glide.load.engine.GlideException: Failed to load resource
There were 2 causes:
java.io.IOException(Cleartext HTTP traffic to books.google.com not permitted)
java.io.FileNotFoundException(No content provider: http://books.google.com/books/content?id=F1wgqlNi8AMC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 2): class com.bumptech.glide.load.engine.GlideException: Fetching data failed,
class java.io.InputStream, REMOTE
There was 1 cause:
So, you can just add the "S" letter to pictures url protocol from the API and make a request through HTTPS.
In my case, this did the trick.

Picasso does not load image url reference, but loads "http://...."

Picasso doesnt load images if I use imageURL reference but loads when I use actual url e.g. "http://i2.cdn.turner.com/cnnnext/dam/assets/161017171526-cafe-neo-cup-super-169.jpg"
String imageURL = feedItem.getImageUrl();
Picasso.with(getContext()).load(imageURL).resize(600, 0).into(newsImage);
Log.i(LOG_TAG,"Image url is: "+imageURL);
Here is the log output from above
10-20 22:32:00.141 13274-13274/bw.co.fus.print I/NewsFeedAdapter: Image url is: "http://i2.cdn.turner.com/cnnnext/dam/assets/161017171526-cafe-neo-cup-super-169.jpg"
Picasso loads when I use this
Picasso.with(getContext()).load("http://i2.cdn.turner.com/cnnnext/dam/assets/161017171526-cafe-neo-cup-super-169.jpg").resize(600, 0).into(newsImage);
I have tried different resize options, including .fit() and without, also .centercrop(). Also thought it could be null but clearly its not.
Please double check if your feedItem#imageUrl variable doesn't have quotation marks on either side. According to your log output, it has. If you are passing URI to Picasso as a String, it should look like
http://whatever.com/...
and not
"http://whatever.com/..."
Also, use debugger to find out more about what's inside of your model for this specific time (beware, if you are using Realm, it will shown as null, here's why)

I am not able to cache the images as I bindind Image using MvvmCross

I am new to Xamarin Android Application.I use Picasso component to cache and download Images and It works fine.
Picasso.With (this.Activity).Load ("Here I pass Url").Into (imageview);
Now I am using MvvmCross binding like:
<Mvx.MvxImageView
android:layout_width="120dp"
android:layout_height="140dp"
android:id="#+id/ProductImageView"
android:scaleType="fitXY"
local:MvxBind="ImageUrl URL" />
Here URL is a string which I set in Viewmodel.My problem is, it downloads image but can not cache that image like picasso does.Can anyone suggest me what to do?
How to use Picasso to bind and cache image ?
There's nothing magic about MvxImageView - it's class a class that inherits from ImageView and exposes a public string ImageUrl property that you can use in binding.
You don't have to use MvxImageView - you can create your own PicassoImageView and expose a property:
private string _i;
public string ImageUrl
{
get { return _i; }
set {
if (_i == value) return;
_i = value;
if (string.IsNullOrEmpty(value)) {
// what do you want to do here? clear the view? use a placeholder?
return;
}
Picasso.With (this.Context).Load (_i).Into (this);
}
}
That sort of thing should work...
...With bonus points if you blog/write about how you get this working and what you learn along the way (or if you publish a nuget package that others can use)
You can cache an image with Picasso without loading it in any ImageViewers like this:
Picasso. with(Context). load(ImageFile.Url). into(null);
But im not sure how you can use this later.
You can try your own implementation of caching, it's easy, check this article

(Glide) Getting image from cache if it exists

I have trouble when I'm using Glide in my app. As I've understood, if image was downloaded once and I request image from cache from other activity, Glide must show image quick. And I got this behavior, but not in my app. Image loads very slow (about 3 seconds), although in another app it was about 0.4 second.
My code with calling Glide:
Glide.with(this)
.load(url)
.signature(new StringSignature(url))
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(mHeader);
And in other activity code are same.
May you help me?
Thanks
You don't need the signature(url) part, the model (url String in your case) is already a part of the cache key.
The problem may be that your header changes size. The view size (= resulting Bitmap size) needs to be constant for a cache hit. However since you're doing ALL caching the load should still be fast. Is there anything changing in the url maybe, like a sessionid or similar? That would make the cache miss.
If the url you're loading is an animated GIF RESULT caching can be the culprit, here's a reference.

Categories

Resources