获取存储在SD卡+ android中的缩略图Uri /图像路径

时间:2011-04-05 07:36:08

标签: android thumbnails

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和路径,但是我无法获得所选图像的缩略图的Uri和路径。

获取原始图像的代码Uri和路径:

@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)我不打算像question那样调整图片大小。我特意寻找由Android OS本身生成的缩略图。

2)使用SDK版本1.6,因此对ThumbnailUtils类不感兴趣。

9 个答案:

答案 0 :(得分:80)

您可以使用它来获取缩略图:

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

有两种类型的缩略图:
MINI_KIND:512 x 384缩略图
MICRO_KIND:96 x 96缩略图

或者使用几乎相同参数的[queryMiniThumbnails] [1]来获取缩略图的路径。

修改

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 [])

答案 1 :(得分:8)

This解决方案对我有用!

final int THUMBSIZE = 128;

Bitmap thumbImage = ThumbnailUtils.extractThumbnail(
                         BitmapFactory.decodeFile(file.getAbsolutePath()), 
                         THUMBSIZE, 
                         THUMBSIZE);

答案 2 :(得分:7)

它可能是其他方式已经在他们的答案中提到的替代方式,但我发现获取缩略图的简单方法是使用ExifInterface

ExifInterface exif = new ExifInterface(pictureFile.getPath());
byte[] imageData=exif.getThumbnail();
Bitmap  thumbnail= BitmapFactory.decodeByteArray(imageData,0,imageData.length);

答案 3 :(得分:5)

两种没有弃用方法的变体。

 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;
}

答案 4 :(得分:4)

基于@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;
}

答案 5 :(得分:2)

答案 6 :(得分:2)

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;
}

答案 7 :(得分:0)

接受的答案对我不起作用。我使用以下方法来实现它:

p <- ggplot(mtcars, aes(x=mpg, y=cyl))+
    geom_bar(stat = "identity")+
    theme_tufte()

tickmarks <- ggplot_build(p)$layout$panel_ranges[[1]]$y.major_source  

p + geom_hline(yintercept = tickmarks, col="white", lwd=3)

答案 8 :(得分:0)

获取缩略图和所有 Android 版本的最佳答案是:

val thumbnail: Bitmap = if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)) {
    mContentResolver.loadThumbnail(contentUri, Size.parseSize(""), null)
} else {
    MediaStore.Images.Thumbnails.getThumbnail(mContentResolver, id, MediaStore.Images.Thumbnails.MINI_KIND, null)
}