Select Thumbnail from Android Gallery

I know how to get photo from gallery in android

Intent gallery = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(gallery, PHOTO_REQUEST_CODE); 

But how exactly to choose a sketch?

REASON FOR BOUNT:

I already tried both solutions on Get Uri thumbnail / path to image stored on SD card + android . They do not work for me. I don't know how to get selectedImageUri , which is of type long , from data to

  onActivityResult(int requestCode, int resultCode, Intent data) 
+7
source share
3 answers
 String fn = ...; // file name ContentResolver cr = ctx.getContentResolver(); Cursor c = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{ BaseColumns._ID }, MediaColumns.DATA + "=?", new String[]{ fn }, null); if(c!=null) { try{ if(c.moveToNext()) { long id = c.getLong(0); Bitmap thumbnail = MediaStore.Images.Thumbnails.getThumbnail(cr, id, MediaStore.Images.Thumbnails.MINI_KIND, null); } }finally{ c.close(); } } 
0
source

If you have a cursor in your hand, you can get its identifier, as

 int id = cursor.getInt(cursor .getColumnIndex(MediaStore.MediaColumns._ID)); 

Repeat the following code

 Cursor cursor = context.getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Images.Media._ID }, MediaStore.Images.Media.DATA + "=? ", new String[] { filePath }, null); if (cursor != null && cursor.moveToFirst()) { int id = cursor.getInt(cursor .getColumnIndex(MediaStore.MediaColumns._ID)); Uri baseUri = Uri.parse("content://media/external/images/media"); return Uri.withAppendedPath(baseUri, "" + id); 

So, for thumbnails,

 Bitmap thumbnail = MediaStore.Images.Thumbnails.getThumbnail(cursor, id, MediaStore.Images.Thumbnails.MINI_KIND, null); 
0
source

Hey, if all else fails for you, this is an easy way to make your own thumbnail if you have a bitmap. If you do not know how to download Bitmap from Uri:

 Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri); 

Here is the code to make a beautiful formatted sketch:

  final int THUMBNAIL_HEIGHT = 75;//48 final int THUMBNAIL_WIDTH = 75;//66 Float width = new Float(bitmap.getWidth()); Float height = new Float(bitmap.getHeight()); Float ratio = width/height; bitmap = Bitmap.createScaledBitmap(bitmap, (int)(THUMBNAIL_HEIGHT*ratio), THUMBNAIL_HEIGHT, false); int padding = (THUMBNAIL_WIDTH - bitmap.getWidth())/2; image.setPadding(padding, padding, padding, padding); image.setBackgroundColor(0); image.setImageBitmap(bitmap); 

In this code, "image" is a variable for ImageView. Hope this helps some: D

0
source

All Articles