从android库中选择缩略图

时间:2013-06-04 22:01:02

标签: android android-intent bitmap thumbnails android-image

我知道如何从android中的相册中获取照片

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

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

BOUNTY的理由:

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

 onActivityResult(int requestCode, int resultCode, Intent data)

3 个答案:

答案 0 :(得分: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();
        }
     }

答案 1 :(得分: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);

答案 2 :(得分: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);

在此代码中,“image”是ImageView的变量。我希望这有助于一些人:D