Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't read thumbnails on Android 10 (loadThumbnail)

I'm reading thumbnails from the device by querying the MediaStore, using MediaStore.Images.Thumbnails.getThumbnail(). However, this has been deprecated in Android 10 (API 29), with a pointer to ContentResolver#loadThumbnail: https://developer.android.com/reference/android/provider/MediaStore.Images.Thumbnails

However, I can't get this to work (in an emulated device running API 29). I've copied some JPEGs onto the emulated device, which end up in the Downloads folder. They show up fine in the Photos app. The following code gives me a FileNotFoundException. What does "No content provider" actually tell me?

try {

    Size thumbSize = new Size(100, 100);
    Uri thumbUri = Uri.fromFile(new File(imgPath));
    // imgPath: /storage/emulated/0/Download/pexels-photo-323490.jpeg
    // thumbUri: file:///storage/emulated/0/Download/pexels-photo-323490.jpeg

    Bitmap thumbBitmap;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        thumbBitmap = mContext.getContentResolver().loadThumbnail(thumbUri, thumbSize, null);
    } else {
        thumbBitmap = MediaStore.Images.Thumbnails.getThumbnail(mContext.getContentResolver(),
                imgId, MediaStore.Images.Thumbnails.MINI_KIND, null);
    }

    iconView.setImageBitmap(thumbBitmap);
} catch (Exception e) {
    Log.e("err", e.toString());
}

Exception:

java.io.FileNotFoundException: No content provider: file:///storage/emulated/0/Download/pexels-photo-323490.jpeg
like image 423
joakimk Avatar asked Feb 20 '20 11:02

joakimk


People also ask

How do I get thumbnails on Android?

Try this. final int THUMBSIZE = 64; Bitmap ThumbImage = ThumbnailUtils. extractThumbnail(BitmapFactory. decodeFile(imagePath), THUMBSIZE, THUMBSIZE);

How do I get the thumbnail from a video URI?

This works for me: Bitmap thumb = ThumbnailUtils. createVideoThumbnail(filePath, Thumbnails. MINI_KIND);


2 Answers

Please try this, hope it works for You:

int thumbColumn = cur.getColumnIndexOrThrow(MediaStore.Images.ImageColumns._ID); 
int _thumpId = cur.getInt(thumbColumn); 
Uri imageUri_t = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,_thumpId);

GGK

like image 70
GGK stands for Ukraine Avatar answered Oct 15 '22 05:10

GGK stands for Ukraine


The best Answer for getting Thumbnail and All Android Versions is this:

val thumbnail: Bitmap = if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)) {
    mContentResolver.loadThumbnail(contentUri, Size(width / 2, height / 2), null)
} else {
    MediaStore.Images.Thumbnails.getThumbnail(mContentResolver, id, MediaStore.Images.Thumbnails.MINI_KIND, null)
}
like image 44
Nafis Kabbo Avatar answered Oct 15 '22 06:10

Nafis Kabbo