获取缩略图Uri /存储在SD卡+ Android的图像的path
SDK版本 – 1.6
我正在使用下面的意图打开Android的默认画廊:
Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult( Intent.createChooser(intent, "Select Picture"), 101);
现在在onActivityResult
,我能够得到所选图像的原始Uri和path,但我无法获得所选图像的缩略图的Uri和path。
获取原始图像的代码Uri和path:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); try { if (requestCode == 101 && data != null) { Uri selectedImageUri = data.getData(); String selectedImagePath = getPath(selectedImageUri); } else { Toast toast = Toast.makeText(this, "No Image is selected.", Toast.LENGTH_LONG); toast.show(); } } catch (Exception e) { e.printStackTrace(); } } public String getPath(Uri uri) { String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = managedQuery(uri, projection, null, null, null); int column_index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); }
PS:1)我不想像这个问题调整图像大小。 我特别寻找由android操作系统本身产生的缩略图。
2)使用SDK版本1.6,所以对ThumbnailUtils类不感兴趣。
你可以用它来获得缩略图:
Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnails( getContentResolver(), selectedImageUri, MediaStore.Images.Thumbnails.MINI_KIND, (BitmapFactory.Options) null );
有两种types的缩略图可用:
MINI_KIND:512 x 384缩略图
MICRO_KIND:96 x 96缩略图
或者使用[queryMiniThumbnails] [1]几乎相同的参数来获取缩略图的path。
编辑
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 [ ])
这个解决scheme是对我的工作!
final int THUMBSIZE = 128; Bitmap thumbImage = ThumbnailUtils.extractThumbnail( BitmapFactory.decodeFile(file.getAbsolutePath()), THUMBSIZE, THUMBSIZE);
这可能是另一种方式,因为其他人已经在他们的答案中提到,但简单的方法,我发现缩略图是使用ExifInterface
ExifInterface exif = new ExifInterface(pictureFile.getPath()); byte[] imageData=exif.getThumbnail(); Bitmap thumbnail= BitmapFactory.decodeByteArray(imageData,0,imageData.length);
两个变种没有depricated方法。
public String getThumbnailPath(Uri uri) { String[] proj = { MediaStore.Images.Media.DATA }; // This method was deprecated in API level 11 // Cursor cursor = managedQuery(contentUri, proj, null, null, null); CursorLoader cursorLoader = new CursorLoader(activity, uri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID); cursor.moveToFirst(); long imageId = cursor.getLong(column_index); //cursor.close(); String result=""; cursor = MediaStore.Images.Thumbnails.queryMiniThumbnail(activity.getContentResolver(), imageId, MediaStore.Images.Thumbnails.MINI_KIND, null); if (cursor != null && cursor.getCount() > 0) { cursor.moveToFirst(); result = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA)); cursor.close(); } return result; } public Bitmap getThumbnailBitmap(Uri uri){ String[] proj = { MediaStore.Images.Media.DATA }; // This method was deprecated in API level 11 // Cursor cursor = managedQuery(contentUri, proj, null, null, null); CursorLoader cursorLoader = new CursorLoader(activity, uri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID); cursor.moveToFirst(); long imageId = cursor.getLong(column_index); //cursor.close(); Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail( getContentResolver(), imageId, MediaStore.Images.Thumbnails.MINI_KIND, (BitmapFactory.Options) null ); return bitmap; }
基于@ Karan的回答和以下评论,仅仅为了到达这里的人(像我一样),需要一个现成的代码:
public String getThumbnailPath(Uri uri) { String[] projection = { MediaStore.Images.Media._ID }; String result = null; Cursor cursor = managedQuery(uri, projection, null, null, null); int column_index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media._ID); cursor.moveToFirst(); long imageId = cursor.getLong(column_index); cursor.close(); cursor = MediaStore.Images.Thumbnails.queryMiniThumbnail( getContentResolver(), imageId, MediaStore.Images.Thumbnails.MINI_KIND, null); if (cursor != null && cursor.getCount() > 0) { cursor.moveToFirst(); result = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA)); cursor.close(); } return result; }
public static String getThumbnailPath(Context context, String path) { long imageId = -1; String[] projection = new String[] { MediaStore.MediaColumns._ID }; String selection = MediaStore.MediaColumns.DATA + "=?"; String[] selectionArgs = new String[] { path }; Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { imageId = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID)); cursor.close(); } String result = null; cursor = MediaStore.Images.Thumbnails.queryMiniThumbnail(context.getContentResolver(), imageId, MediaStore.Images.Thumbnails.MINI_KIND, null); if (cursor != null && cursor.getCount() > 0) { cursor.moveToFirst(); result = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA)); cursor.close(); } return result; }
接受的答案是不适合我的。 我用下面的方法来做到这一点:
try{ Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getActivity().getContentResolver(), uri); Bitmap thumbBitmap = ThumbnailUtils.extractThumbnail(bitmap,120,120); // imageView.setImageBitmap(thumbBitmap); } catch (IOException ex){ //...... }