Android: Get Thumbnail of Image on Sd Card, Given Uri of Original Image

Get thumbnail Uri/path of the image stored in sd card + android

You can use this to get the thumbnail:

Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(
getContentResolver(), selectedImageUri,
MediaStore.Images.Thumbnails.MINI_KIND,
(BitmapFactory.Options) null );

There are two types of thumbnails available:

MINI_KIND: 512 x 384 thumbnail
MICRO_KIND: 96 x 96 thumbnail

OR use [queryMiniThumbnails][1] with almost same parameters to get the path of the thumbnail.

EDIT

Cursor cursor = MediaStore.Images.Thumbnails.queryMiniThumbnails(
getContentResolver(), selectedImageUri,
MediaStore.Images.Thumbnails.MINI_KIND,
null );
if( cursor != null && cursor.getCount() > 0 ) {
cursor.moveToFirst();//**EDIT**
String uri = cursor.getString( cursor.getColumnIndex( MediaStore.Images.Thumbnails.DATA ) );
}

HTH !

[1]: https://developer.android.com/reference/android/provider/MediaStore.Images.Thumbnails.html#queryMiniThumbnails(android.content.ContentResolver, android.net.Uri, int, java.lang.String[])

Generate Thumbnail From Sdcard in Android Q

This is solution for generate thumbnail of video from storage.

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {

thumbnail_bitmap = createThumbnail(RecordPitchActivity.this, path);

} else {
thumbnail_bitmap = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);
}
public static Bitmap createThumbnail(Activity activity, String path) {
MediaMetadataRetriever mediaMetadataRetriever = null;
Bitmap bitmap = null;
try {
mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(activity, Uri.parse(path));
bitmap = mediaMetadataRetriever.getFrameAtTime(1000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (mediaMetadataRetriever != null) {
mediaMetadataRetriever.release();
}
}
return bitmap;
}

Getting Thumbnails from sdcard with URI

The following method can be used to get the thumbnail of image:

private Bitmap getBitmap(String path) {

Uri uri = getImageUri(path);
InputStream in = null;
try {
final int IMAGE_MAX_SIZE = 1200000; // 1.2MP
in = mContentResolver.openInputStream(uri);

// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, o);
in.close();

int scale = 1;
while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) > IMAGE_MAX_SIZE) {
scale++;
}
Log.d(TAG, "scale = " + scale + ", orig-width: " + o.outWidth + ", orig-height: " + o.outHeight);

Bitmap b = null;
in = mContentResolver.openInputStream(uri);
if (scale > 1) {
scale--;
// scale to max possible inSampleSize that still yields an image
// larger than target
o = new BitmapFactory.Options();
o.inSampleSize = scale;
b = BitmapFactory.decodeStream(in, null, o);

// resize to desired dimensions
int height = b.getHeight();
int width = b.getWidth();
Log.d(TAG, "1th scale operation dimenions - width: " + width + ", height: " + height);

double y = Math.sqrt(IMAGE_MAX_SIZE
/ (((double) width) / height));
double x = (y / height) * width;

Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, (int) y, true);
b.recycle();
b = scaledBitmap;

System.gc();
} else {
b = BitmapFactory.decodeStream(in);
}
in.close();

Log.d(TAG, "bitmap size - width: " +b.getWidth() + ", height: " + b.getHeight());
return b;
} catch (IOException e) {
Log.e(TAG, e.getMessage(),e);
return null;
}
}

And always call bitmap.recycle() method after using bitmaps. It will clear the bitmap from memory. Also avoid memory leaks in your code. This will solve your OOME.

Camera intent returns thumbnail, but want to tap on thumbnail to display original image

In onMarkerClick method you create Uri this way Uri.parse("bitmap" + "data"). So in the end you're trying to show picture with Uri bitmapdata. You should pass correct Uri of the picture to Uri.parse method. You're saving your picture under destinationFile so make a File destinationFile a class member field and get Uri through Uri.parse(destinationFile.toString()).

If you want to display each picture for a given Marker correctly then you have to store Uris of all files to which you've saved pictures. For example you can have a Map<Integer, Uri> in which keys will be Markers ids and values will be pictures Uris. Then in onMarkerClick get the id of clicked marker through Marker.getId() and use it to get appropriate Uri for picture from Map.

android how to get particular image present in sd card

Here, you can show all images in SDCard and when you click one of them, you get the path of selected image:

try {
// Set up an array of the Thumbnail Image ID column we want
String[] projection = { MediaStore.Images.Thumbnails._ID };
// Create the cursor pointing to the SDCard
cursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null, MediaStore.Images.Thumbnails.IMAGE_ID);

// Get the column index of the Thumbnails Image ID
columnIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

GridView sdcardImages = (GridView) findViewById(R.id.gallary);
sdcardImages.setAdapter(new ImageAdapter(this));

// Set up a click listener
sdcardImages.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,
int position, long id) {
// Get the data location of the image
String[] projection = { MediaStore.Images.Media.DATA };
cursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null, null);
columnIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToPosition(position);
// Get image filename
String imagePath = cursor.getString(columnIndex);
// Use this path to do further processing, i.e. full screen
// display
}
});
} catch (Exception e) {
e.printStackTrace();
}

And here is the ImageAdapter:

/**
* Adapter for our image files.
*/
private class ImageAdapter extends BaseAdapter {

private Context context;

public ImageAdapter(Context localContext) {
context = localContext;
}

public int getCount() {
return cursor.getCount();
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
// Set the content of the image based on the provided URI
picturesView.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""
+ imageID));
picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
picturesView.setPadding(8, 8, 8, 8);
picturesView
.setLayoutParams(new GridView.LayoutParams(100, 100));
} else {
picturesView = (ImageView) convertView;
}
return picturesView;
}
}

And if you know the exact path and image name, you can get Bitmap by using:

Bitmap bitmap = BitmapFactory.decodeFile("yourImageDirectory" + "yourImageName.png");


Related Topics



Leave a reply



Submit