Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

Get thumbnail Uri/path of the image stored in sd card + android

SDK version - 1.6

I am using following intent to open android's default gallery:

Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(
                        Intent.createChooser(intent, "Select Picture"), 101);

Now in onActivityResult, i am able to get the original Uri and path of the selected image, but i am not able to get the Uri and path of the thumbnail of selected image.

Code for getting the original image Uri and 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) i am not looking to resize image like this question. I am specifically looking for the thumbnails which are generated by android OS itself.

2) Using SDK version 1.6 so not interested in ThumbnailUtils class.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use this to get the thumbnail:

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

There are two types of thumbnails available:
MINI_KIND: 512 x 384 thumbnail
MICRO_KIND: 96 x 96 thumbnail

OR use [queryMiniThumbnails][1] with almost same parameters to get the path of the thumbnail.

EDIT

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...