Android getting thumbnail Uri after getting picture from the Camera

2.2k views Asked by At

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
                .getColumnIndexOrThrow(MediaStore.Images.Media._ID);

        cursor.moveToFirst();
        long imageId = cursor.getLong(column_index);
        cursor.close();

        Cursor cursor2 = MediaStore.Images.Thumbnails.queryMiniThumbnail(
                getContentResolver(), imageId,
                MediaStore.Images.Thumbnails.MINI_KIND,
                null);
        if (cursor2 != null && cursor2.getCount() > 0) {
            cursor2.moveToFirst();
            result = cursor2.getString(cursor2.getColumnIndexOrThrow(MediaStore.Images.Thumbnails
                    .DATA));
            cursor2.close();
        }
    }
    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());
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            takePicUri);
    startActivityForResult(intent, OPTION_TAKE_PHOTO_FROM_CAMERA);
}


private File getTempFile(Context context) {
     final File path = new  File(Environment.getExternalStorageDirectory(),
            context.getPackageName());
    if (!path.exists()) {
        path.mkdir();
    }
    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.

2

There are 2 answers

3
sahu On

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);
}
@Override
    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");



                imageView.setImageBitmap(myBmp);
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();

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

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

                break;
            }
        }
        }
    }

Hope this will help you.

1
greenapps On

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");
imageView1.setImageBitmap(bitmap);