How to load thumbnail with Universal Image Loader for Android? - android

I am using this lib:
https://github.com/nostra13/Android-Universal-Image-Loader
I want to load an image thumbnail into an ImageView using the lib.
What content uri can I pass into:
imageLoader.displayImage(imageUri, imageView); //?
The docs give an example of using a content url:
String imageUri = "content://media/external/audio/albumart/13"; // from content provider
I pretty much want to switch from doing this:
return MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
to using the lib.
As you can see I have the id but I need to construct a url for MICRO_KIND image thumbnails...

I think you can use next URI format: content://media/external/images/thumbnails/13
String uri = "content://media/external/images/thumbnails/" + id;

Related

Is there any way to create Uri from bitmap but i don't want to see it in image gallery?

Scenario: I am using MessagingStyle notification in a chat application. For image messages, I am using setData function to create image notification in conversation style.
public Message setData(String dataMimeType, Uri dataUri)
This method required dataUri but I have URL of image so I have created a bitmap from image url and then create a uri from batmap using function below.
public static Uri getImageUri(Context applicationContext, Bitmap photo) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(applicationContext.getContentResolver(), photo, "IMG_" + Calendar.getInstance().getTime(), null);
if (path == null) {
return null;
}
return Uri.parse(path);
} catch (SecurityException ex) {
ex.printStackTrace();
return null;
}
}
Problem: Now the problem is that when user open image gallery, these notifications images are showing because I used mediastore to create URI. I don’t want these image to show in gallery.
Limitation: I am also saving that URI in case If I have to generate this notification again. So I can’t delete that image immediately to avoid showing in gallery.
Question: Is there any way to save image privately in external storage where gallery don’t have access. But notification manager can access?
Is there any way to create Uri form url without downloading it? I have tried this but it also not working
url = new URL(messageNotification.getAttachmentImage());
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
imageUri= Uri.parse(uri.toString());
Another workaround to show image in notification by using Messagings style like conversation as in WhatsApp
Download your image directly from url to a file in getExternalFilesDir(). Dont use an intermediate bitmap.
The MediaStore has no access to your getExternalFilesDir() so Gallery apps dont know about your files there.
The user can still use the Files app to see those images.
Then use a FileProvider to serve your files. Construct an uri with FileProvider.getUriForFile().
store your bitmap as File in some app-specific directory (not gallery) and make URI from file path. be aware of Scoped Storage and pick proper place for storing Bitmaps - check out in HERE, probably getFilesDir()/getCacheDir() method will be the best option
also worth trying may be this line:
Uri uri = Uri.parse(messageNotification.getAttachmentImage());
(encodedImageUrl = URLEncoder.encode(imageUrl, "utf-8") may be needed)

Can't setImageURI(). Why?

I want to add images to the ViewHolder. I do everything in method onBIndViewHolder(). I get String variable path from db, try to parse it by Uri and setImageUri, but it doesn't work. For debuggind I log path to the console.
String imgPath = images.get(position);
Log.d("imgTag", imgPath);
Uri imageURI = Uri.parse(imgPath);
//holder.image.setImageURI(imageURI);
Path of the image from log:
/document/image:46277
If I set image by setImageResource() it works fine, but I need from URI.

How to get Video thumbnail from URL

I am trying to generate a thumbnail of the video. But I cannot use Glide as it colliding with other gallery libraries.
I have tried Picasso that is not giving me thumbnail from the URL. ThumbnailUtils.createVideoThumbnail is working in my code but it is too slow.
Can someone suggest any effective tool or technique, to get the thumbnail from the URL in java??
You can use the MediaMetadataRetriever class. For example
private fun getThumbNail(uri: Uri): Bitmap {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(context, uri)
return retriever.frameAtTime //or frameAtPosition(..)/frameAtIndex(..)
}

Universal Image Loader and BitmapDrawable

We have an image in B64, an we want to use that image as loading image in Universal Image Loader while a better image isn't loaded from an url. So we create a bitmap from that url in b64 and convert it into a BitmapDrawable.
The result is shown fine if I do:
imageView.setImageDrawable(bitmapDrawable)
But, on DisplayImageOptions, if I set that bitmapDrawable as image on loading, the image is never shown. I'm doing the following:
final DisplayImageOptions imageOptions = mDisplayImageOptionsDefaultBuilder.
.showImageOnLoading(bitmapDrawable)
.showImageOnFail(bitmapDrawable)
.showImageForEmptyUri(bitmapDrawable)
.build()
As you can see, I am setting the bitmap drawable not only as image when loading, but as image when fails too (as we don't want that image to change in case of error while loading the better image from an url). The result of that is that the bitmap drawable is never shown. What are we doing wrong?
UPDATE:
After debugging what was happening I've seen that the problem is not the bitmap drawable, it is supported and working fine. The problem was that I am using a default display options builder (mDisplayImageOptionsDefaultBuilder), than at some point did:
final DisplayImageOptions imageOptions = mDisplayImageOptionsDefaultBuilder.
.showImageOnLoading(loadingResource)
.showImageOnFail(errorResource)
.showImageForEmptyUri(errorResource)
.build()
So there's a bug in Universal Image Loader, because now I'm creating a display image options with:
.showImageOnLoading(bitmapDrawable)
Another "solution" is do:
final DisplayImageOptions imageOptions = mDisplayImageOptionsDefaultBuilder.
.showImageOnLoading(0)
.showImageOnLoading(loadingResource)
.showImageOnFail(errorResource)
.showImageForEmptyUri(errorResource)
.build()
But internally it stores that there's a resource stored, so my drawable is not shown but the stored resource instead. Creating a new DisplayImageOptionsBuilder worked for me, but it would be nice that if the showImageOnLoading is set with a drawable, then the old resource was automatically cleared.
Thanks in advance.
UIL only supports the following schemes:
"h t t p://site.com/image.png" // from Web
"file:///mnt/sdcard/image.png" // from SD card
"file:///mnt/sdcard/video.mp4" // from SD card (video thumbnail)
"content://media/external/images/media/13" // from content provider
"content://media/external/video/media/13" // from content provider (video thumbnail)
"assets://image.png" // from assets
"drawable://" + R.drawable.img // from drawables (non-9patch images)
Use these schemes.
Universal Image Loader also provide to use the Background functionality.Please check the below coed for it:-
Here Uri is the path of the folder image OR the URL of the image.
imageLoader.loadImage(YOUR_URL, new SimpleImageLoadingListener() {
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
layout.setBackgroundDrawable(new BitmapDrawable(loadedImage));
}
});

Android get image path from drawable as string

Is there any way that I can get the image path from drawable folder in android as String. I need this because I implement my own viewflow where I'm showing the images by using their path on sd card, but I want to show a default image when there is no image to show.
Any ideas how to do that?
These all are ways:
String imageUri = "drawable://" + R.drawable.image;
Other ways I tested
Uri path = Uri.parse("android.resource://com.segf4ult.test/" + R.drawable.icon);
Uri otherPath = Uri.parse("android.resource://com.segf4ult.test/drawable/icon");
String path = path.toString();
String path = otherPath .toString();
based on the some of above replies i improvised it a bit
create this method and call it by passing your resource
Reusable Method
public String getURLForResource (int resourceId) {
//use BuildConfig.APPLICATION_ID instead of R.class.getPackage().getName() if both are not same
return Uri.parse("android.resource://"+R.class.getPackage().getName()+"/" +resourceId).toString();
}
Sample call
getURLForResource(R.drawable.personIcon)
complete example of loading image
String imageUrl = getURLForResource(R.drawable.personIcon);
// Load image
Glide.with(patientProfileImageView.getContext())
.load(imageUrl)
.into(patientProfileImageView);
you can move the function getURLForResource to a Util file and make it static so it can be reused
If you are planning to get the image from its path, it's better to use Assets instead of trying to figure out the path of the drawable folder.
InputStream stream = getAssets().open("image.png");
Drawable d = Drawable.createFromStream(stream, null);
I think you cannot get it as String but you can get it as int by get resource id:
int resId = this.getResources().getIdentifier("imageNameHere", "drawable", this.getPackageName());
First check whether the file exists in SDCard. If the file doesnot exists in SDcard then you can set image using setImageResource() methodand passing default image from drawable folder
Sample Code
File imageFile = new File(absolutepathOfImage);//absolutepathOfImage is absolute path of image including its name
if(!imageFile.exists()){//file doesnot exist in SDCard
imageview.setImageResource(R.drawable.defaultImage);//set default image from drawable folder
}

Categories

Resources