7

我知道如何从 android 的画廊中获取照片

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

但是我将如何专门选择缩略图?

赏金的原因:

我已经在Get thumbnail Uri/path of the image stored in sd card + android尝试了这两种解决方案。他们不适合我。我不知道如何从in 中获取selectedImageUri类型longdata

 onActivityResult(int requestCode, int resultCode, Intent data)
4

3 回答 3

0

如果你手上有它的光标,你可以得到它的 ID,

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

参考以下代码

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

所以,对于缩略图,

Bitmap thumbnail = MediaStore.Images.Thumbnails.getThumbnail(cursor, id, MediaStore.Images.Thumbnails.MINI_KIND, null);
于 2013-06-16T06:26:53.870 回答
0
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();
        }
     }
于 2013-06-11T09:40:24.367 回答
0

嘿,所以如果其他一切都不适合你,如果你有位图,这里是制作自己的缩略图的简单方法。如果您不知道如何从 Uri 加载位图:

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

这是制作漂亮的格式化缩略图的代码:

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

在此代码中,“图像”是 ImageView 的变量。我希望这会有所帮助:D

于 2013-06-17T14:51:52.977 回答