Not able to display gif from local uri using glide - android

Thanks in advance,
I am trying to show a gif image in my imageView . Gif image exists locally in my device.
ImageView imageView = (ImageView) findViewById(R.id.img);
File file = new File(Environment
.getExternalStorageDirectory().toString(), "sample.gif");
Uri uri = Uri.fromFile(file);
Glide.with(getApplicationContext()).load(uri).into(imageView);
The file path is correct, still not able to see the gif loading.

If you are using gif image from raw folder than please use below code
ImageView imageView = (ImageView) findViewById(R.id.imageView);
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(imageView);
Glide.with(this).load(R.raw.sample_gif).into(imageViewTarget);
If you are using any other url for loading gif image.
Glide.with(context)
.load(imageUrl)
.asGif()
.placeholder(R.drawable.loading2)
.crossFade()
.into(imageView);
Keep in mind that just using load() will load either a GIF or a Bitmap depending on the type of the data. Unless you want your load to fail if the given url is not a gif, you don't need to specify asGif()..
Hope this help u...inform me if u need any other help

Related

Is there any way to create thumbnail from video URL?

Want to create tumbnails from url from any sites.
I am using following code
Bitmap bmThumbnail;
bmThumbnail = ThumbnailUtils.createVideoThumbnail(videoLink,
MediaStore.Video.Thumbnails.MICRO_KIND);
But its not working,please suggest any other ways if present.
Use Glide : https://github.com/bumptech/glide
Shortest Method To get Video thumbnails
RequestOptions requestOptions = new RequestOptions();
requestOptions.isMemoryCacheable();
Glide.with(context).setDefaultRequestOptions(requestOptions).load("Url").into(image);
If there is youtube url, then we have to make static url to make it thumbnail. https://img.youtube.com/vi/nE4PBsClUrY/0.jpg
nE4PBsClUrY - this is id of video
Using that we can make a thumbnail.

Android cannot load image from URI using Picasso

The Android app allows users to select a photo from their phone gallery which I save the URI to realm. I then retrieve this information and use Picasso to load it into the image view. For some reason the image is not loaded.
The URI is something like:
content://com.android.providers.media.documents/document/image%3A333180
I save it to realm using mCategory.icon = imageURI.toString() and then when I load it:
Picasso.with(getContext())
.load(Uri.parse(mCategory.icon)) // mCategory.icon is a string
.resize(200, 200)
.error(R.drawable.mountain) // default image to load
.into(viewHolder.categoryIcon);
The URI you got is a ContentProvider URI to show it with Picasso you try to do this:
File file = new File(Uri.parse(mCategory.icon));
Picasso.with(getContext())
.load(file)
.resize(200, 200)
.error(R.drawable.mountain)
.into(viewHolder.categoryIcon);
P.S: Though you can convert your ContentProveder path to Absolute path still it's better if you don't since Picasso support loading from the File..
Use setLoggingEnabled(true) in picasso to check the log if there is any error message.
Mentioned URI contains the encoded value of colon : i.e. %3A.
Either rename the source OR try Glide or some other library to serve the purpose.
This is working perfectly with new version of Picasso 2.71828
Picasso.get()
.load(imageUri)
.placeholder(R.drawable.placeholder)
.fit()
.centerCrop()
.into(viewHolder.categoryIcon);

What is the proper URL to use when loading a gif from Imgur to use in Glide?

I'm looking into using Glide to access gifs off an image hosting website instead of having them all stored in the app within the assets folder. I have Glide working in the sense that it will load the image I specify when the original URL fails.
ImageView imageViewGif = (ImageView) findViewById(R.id.gif);
String gifUrl = "http://imgur.com/gallery/XgbZdeA";
Glide
.with(this)
.load(gifUrl)
.asGif()
.error(R.drawable.alternator_pic)
.into(imageViewGif);
So it loads up alternator pic just fine but I am unable to get it to load an image or gif from the specified URL. What am I missing?

How to refresh the content of an ImageView

I have a screen with an ImageView containing the actual profile picture. I can edit that profile picture either by taking a picture with the camera or by picking a picture from the sd card. I store the new chosen profile picture under the same path as the old (i overwrite it) which is logical i guess.
However when i set a new profile picture to my ImageView it does not get refreshed. I have to restart the App to see the change.
this.imageView.invalidate();
is what all people are telling me to do when i brows google but no, this is not working! So how can i force my imageview to load the new profile picture?
I load the image into the ImageView with help of Picasso:
Picasso picasso = Picasso.with(context);
if(reload) {
picasso.invalidate(new File(fileName));
}
RequestCreator requestCreator = picasso.load(new File(fileName));
requestCreator.into(imageView);
Picasso caches loaded images to speed up the loading process. This means that if you are trying to reload the same exact picture it loads the cached version and it does not read the changed File from the disk.
You need to tell Picasso that the picture has changed and that it needs to reload the picture from the disk by invalidating the cache. The way you do this is like this:
File file = new File(fileName);
Picasso picasso = Picasso.with(context);
picasso.invalidate(file);
picasso.load(file).into(imageView);
To skip Picasso's cache, you can use .memoryPolicy(MemoryPolicy.NO_CACHE) static method:
Picasso
.with(context)
.load(file)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.into(imageView);

Picasso load drawable resources from their URI

I have to show a drawable from res into an ImageView. In this app, I'm using Picasso for some reasons.
In this case, I need to load the drawable using its URI and not its id.
To do that, here is my code:
uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+context.getPackageName()+"/drawable/" + drawableName);
where drawableName used here are file names rather than their resource ids.
Then
Picasso.with(context).load(uri).into(imageView);
I know for sure that drawable name is correct, but Picasso seems it does not like this uri.
If the images is in your drawable folder then you can just load it.
Picasso.with(context).load(R.drawable.drawableName).into(imageView);
and picasso will load it no need for an Uri.
Found the answer. Unfortunately, Picasso do not allow drawable loading via URI. It is an incoming feature.
This is if you don't want to hardcode the image that you are going
to load...
You can load local image files from your drawable folder lazily if you know the integer value of the image that you want to be loaded.
Then you can just do:
Picasso.with(getContext()).load(imageResourceId)
.error(R.drawable.ic_launcher)
.into(imageView);
Where
imageView
is the view you wish to display the image. For example:
imageView = (ImageView) convertView
.findViewById(R.id.itemImage);
And where
imageResourceId
is the integer value of the drawable. You can retrieve this integer value by:
int productImageId = resources.getIdentifier(
productImageName, "drawable", context.getPackageName());
as well as
productImageName
is the name of the drawable you want to draw (i.e. "ic_launcher")
THIS CAN ALL BE DONE INSIDE FROM THE ADAPTER
From picasso v2+ here is a big modification. The new version is very helpful in order to manage image cache data. It's using Singleton Instance.
GRADLE
implementation 'com.squareup.picasso:picasso:2.71828'
Set drawable image
Picasso.get()
.load(url)
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.user_placeholder_error)
.into(imageView);
Bonus, get drawable by name:
public static int getDrawableIdFromFileName(Context context, String nameOfDrawable) {
return context.getResources().getIdentifier(nameOfDrawable, "drawable", context.getPackageName());
}
As mentioned in the documentation of Picasso .
they are now supporting loading Image from URI like the following :
load(android.net.Uri uri)
so you have to do something like the following :
Picasso.with(context).load(uri).into(imageView);
just like what you are doing already .
Hopethat helps .

Categories

Resources