注册 | 登录

Android getting thumbnail Uri after getting picture from the Camera

itPublisher 分享于



I do not want to use any bitmap resizing code if I can help it. I have a method to get a MINI thumbnail uri when I get pictures from the Gallery but when I try to do the same with the Camera it does not work:

Here are the methods:

private Uri getParsedUri(Uri selectedImageUri) {
    Uri parsed = null;
    try {
        String uri = getThumbnailPath(selectedImageUri);

        if (uri != null) {
            Timber.d("Thumb uri found %s", uri);
            File file = new File(uri);
            parsed = Uri.fromFile(file);
            Timber.d("thumb uri parsed : %s", parsed);
        } else {
            Timber.d("Thumb uri not found, using :  %s", selectedImageUri);
    } catch (Exception e){
        Timber.e(e, "Error getting mini uri");
    return parsed;

private String getThumbnailPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media._ID };
    String result = null;
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
    if (cursor != null && cursor.getCount() > 0) {
        int column_index = cursor

        long imageId = cursor.getLong(column_index);

        Cursor cursor2 = MediaStore.Images.Thumbnails.queryMiniThumbnail(
                getContentResolver(), imageId,
        if (cursor2 != null && cursor2.getCount() > 0) {
            result = cursor2.getString(cursor2.getColumnIndexOrThrow(MediaStore.Images.Thumbnails
    return result; //always null for pictures from the Camera

Here is how I provide the uri extra for the Camera app:

private void onTakePhotoFromCameraClick() {
    final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//        takePicUri = Uri.fromFile(getTempFile(ProductNewActivity.this));
    takePicUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            new ContentValues());
    startActivityForResult(intent, OPTION_TAKE_PHOTO_FROM_CAMERA);

private File getTempFile(Context context) {
     final File path = new  File(Environment.getExternalStorageDirectory(),
    if (!path.exists()) {
    long millis = new Date().getTime();
    String name_file = String.valueOf(millis);
    File tempImageFile = new File(path, "MyImage_" + name_file + ".tmp");
    return tempImageFile;

both of the methods of getting the Uri (either using content resolver or creating my custom Uri, do not work and I am never able to get a thumbnail. Is there a way to indicate the system to generate a thumbnail? Is the thumbnail always generated?.

Edit: I think I did not explain clearly. I need 2 Uri: one for big image and one for a thumbnail. I do not need the bitmaps. I am using Universal Image Loader to display the images so I do not need the small bitmap. I generate the Uri for the big image myself but I would like to find a way for Android to generate the thumbnail since it already does it for pictures of the gallery.

android uri thumbnails android-camera-intent
  this question
edited Jun 10 '15 at 13:00 asked Jun 10 '15 at 11:27 vallllll 1,441 2 25 62      Why vote down? Could you please explain why you voted down so I can correct what is wrong? –  vallllll Jun 10 '15 at 11:35


2 Answers

I have this piece of code which may helpful for you,Let me tell you this code will invoke camera and take a picture stored in external directory with full resolution and you can refer this path any time.

final int TAKE_PHOTO_REQ = 100;
    String file_path = Environment.getExternalStorageDirectory()
            + "/recent.jpg";
    file = new File(file_path);
public void takePic(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, TAKE_PHOTO_REQUEST_FRAG);
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case TAKE_PHOTO_REQ: {
            if (resultCode == TakePicture.RESULT_OK && data != null) {

                Bitmap myBmp = (Bitmap) data.getExtras().get("data");

                ByteArrayOutputStream bytes = new ByteArrayOutputStream();

                // you can create a new file name "recent.jpg" in sdcard folder.
                File f = new File(file_path);
                try {
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                // write the bytes in file
                FileOutputStream fo = null;
                try {
                    fo = new FileOutputStream(f);
                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                try {
                } catch (IOException e) {
                    // TODO Auto-generated catch block

                // remember close de FileOutput
                try {
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                Log.e("take-img", "Image Saved to sd card...");


Hope this will help you.

  this answer
answered Jun 10 '15 at 11:38 sahulab 568 1 16      I do not want a bitmap, I want an Uri for small images. –  vallllll Jun 10 '15 at 11:39      Try to get the uri from the path itself.@vallllll –  sahulab Jun 10 '15 at 11:42      Yes but it creates an extra file and an extra bitmap which I do not need. I was wondering if there was a way to get the thumbnail that Android creates by default. –  vallllll Jun 10 '15 at 13:02      Hope we will get the solution soon thanks @vallllll –  sahulab Jun 10 '15 at 13:24


If you are interested only in a thumbnail then start the intent as follows:

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_THUMB );

Then in OnActivityResult you can extract the thumb from the intent like:

Bundle extras = data.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");

  this answer
answered Jun 10 '15 at 12:38 greenapps 6,053 2 6 13      Thanks but on Samsung Galaxu S5 I get null extra –  vallllll Jun 10 '15 at 14:56









您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱