Guide: Android: Use Camera Activity for Thumbnail and Full Size Image:

Today I have decided to show you how to use the build in camera Activity to
Take thumbnail and full size images. The reason for that is that this is a common problem
and many people get confused as for why do they get a low resolution image when they are
using the build in camera Activity for taking images in their Android application
(I was one of those people).

But first I would like to stop and explain why do you need thumbnail images in your app.
The problem with mobile devices as we all know is that they are low on resources (CPU,
Memory, Storage, etc.. today clearly this statement becomes less and less true), as for this
reason the Android OS was designed to allow a single application use only a very limited
amount of memory at run time.

That means that you can’t take a full 5 or 8 megapixels image and load it into memory just to
show it inside a small ImageView. Well, you can, but on the second or third image your
application will simply crash with an java.lang.OutOfMemoryError. So clearly you have
to care always to use the image with the most low resolution but yet satisfies with it quality at run time.

Back to where I started, people get confused because there are several ways to perform this
operation and I will show you them today:

Thumbnail Image:

Actually this will be the easiest way to take an image using the camera and to save it in
you application. The problem with this is that this image is very low resolution and is
not fit for displaying on a full mobile screen:

1. First of all define:


This will be your code so you could read the result of the camera Activity. You can
define here any number you would like.

2. Next fire an intent that will start an Activity for result.

Intent intent = new Intent("");

3. Finally receive the result and save it:

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    //Check that request code matches ours:
        //Check if your application folder exists in the external storage, if not create it:
        File imageStorageFolder = new File(Environment.getExternalStorageDirectory()+File.separator+"Your application Folder");
        if (!imageStorageFolder.exists())
            Log.d(TAG , "Folder created at: "+imageStorageFolder.toString());

        //Check if data in not null and extract the Bitmap:
        if (data != null)
            String filename = "image";
            String fileNameExtension = ".jpg";
            File sdCard = Environment.getExternalStorageDirectory();
            String imageStorageFolder = File.separator+"Your application Folder"+File.separator;
            File destinationFile = new File(sdCard, imageStorageFolder + filename + fileNameExtension);
            Log.d(TAG, "the destination for image file is: " + destinationFile );
            if (data.getExtras() != null)
                Bitmap bitmap = (Bitmap)data.getExtras().get("data");
                    FileOutputStream out = new FileOutputStream(destinationFile);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                catch (Exception e) 
                    Log.e(TAG, "ERROR:" + e.toString());

You can see that in this case we are getting our image from the Extras of the intent under
the “data” key. We cast it to a Bitmap and save it to a file using an FileOutputStream.
But actually we didn’t had to save it to file and could use the bitmap straight forward.

Full Size Image:

Well the big difference here is if we want to produce a high quality image from the
camera Activity we don’t have a choice but to save it into file and then use:

1. So First of all as before we need to create a static int that will be our requestCode:


2. Next we fire again the same intent to start Activity for result but this time with a little difference:

Intent intent = new Intent("");
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));

and the difference in this case is with this line: intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));

Here we are actually passing an URI as an extra to the intent in order to save the image at this location
when it will be taken.

3. Finally we will receive the result in onActivityResult:

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    //Check that request code matches ours:
        //Get our saved file into a bitmap object:
       File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
       Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);

When decodeSampledBitmapFromFile method is:

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) 
    //First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize, Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    int inSampleSize = 1;

    if (height > reqHeight) 
        inSampleSize = Math.round((float)height / (float)reqHeight);
    int expectedWidth = width / inSampleSize;

    if (expectedWidth > reqWidth) 
        //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
        inSampleSize = Math.round((float)width / (float)reqWidth);

    options.inSampleSize = inSampleSize;

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeFile(path, options);

For both cases don’t forget to add the relevent camera permissions to the manifest file:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="" />
<uses-feature android:name="" />

And that’s it, now you can add the build in camera capabilities to your application and take
images with the quality you want. Enjoy.

27 Responses to this entry

  • Rafael Jacobo Says:

    Thanks man, that work perfectly form me (Y)

    Posted on November 1st, 2013 at 10:26 pm Reply | Quote
  • Divya Says:

    Thankyou for guiding in a perfect way. Its working properly. I read for camera on various sites but found this helpful.

    Posted on May 27th, 2014 at 6:45 pm Reply | Quote
  • vahid Says:


    Posted on June 2nd, 2014 at 6:46 pm Reply | Quote
  • Marissa Says:

    Works perfectly. Thanks for the great tutorial.

    Posted on June 27th, 2014 at 10:48 pm Reply | Quote
  • Tushar Says:

    Image comes out to be rotated by 90 degress. Solution?

    Posted on July 22nd, 2014 at 9:27 am Reply | Quote
  • Emil Adjiev Says:

    @Tushar Well you could try to just rotate it. you could try this link for help:
    Android: Rotate image in ImageView by 90degrees but without delay

    Posted on July 23rd, 2014 at 5:29 am Reply | Quote

    its really qualitative blog ,thanks

    Posted on September 26th, 2014 at 3:42 pm Reply | Quote
  • priyank Says:

    Thanks man… Its very useful code….

    Posted on September 29th, 2014 at 2:57 pm Reply | Quote
  • priyank Says:

    @Tushar I also suffering form this problem have u solve your problem??

    Posted on September 29th, 2014 at 2:58 pm Reply | Quote
  • Emil Adjiev Says:

    @priyank hi, Have you tried the solution I provided for @Tushar one comment bellow?

    Posted on September 30th, 2014 at 3:15 pm Reply | Quote
  • Gannicus Says:

    Thank you for this great article, I couldn’t find something like it elsewhere.
    However, I have some trouble trying to choose the size of the photo.

    When I take a photo in portrait mode, it’s height is always too high when I display it on my activity. Even if I double the width when I call DecodeSampledBitmapFromFile.

    Can you help me please?

    Posted on October 15th, 2014 at 2:08 am Reply | Quote
  • Anas Says:

    Please, Would it be possible that you put all the code in github, so we can download it

    Posted on April 26th, 2015 at 3:04 am Reply | Quote
  • Emil Adjiev Says:

    @Anas Hey, I’m sorry but this is not possible. This code is a part of a bigger project that belongs to a company I worked at. So this code belongs to them and con’t be published. What more I don’t even have access to it anymore. So sorry, but this could not be done.

    Posted on May 3rd, 2015 at 7:09 pm Reply | Quote
  • Fodil Says:

    Thank you for this useful tutorial

    Posted on May 18th, 2015 at 4:23 pm Reply | Quote
  • Wildan Alfarobi Says:

    thanks mate, you save my life. Keep coding

    Posted on September 21st, 2015 at 6:58 am Reply | Quote
  • Rizki Maulana Says:

    Thanks, this is work perfectly/

    Posted on February 4th, 2016 at 3:59 am Reply | Quote
  • Milad Says:

    Thank you verrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrry much.

    Posted on February 19th, 2016 at 6:33 pm Reply | Quote
  • Boateng Says:

    Almost four years down the line and this post is still relevant. Thanks man, you saved me days of labour

    Posted on March 4th, 2016 at 12:36 pm Reply | Quote
  • Gabriel Says:

    You’re the best dude!

    Posted on April 20th, 2016 at 12:29 pm Reply | Quote
  • Taman Says:

    Cool man. savior

    Posted on April 22nd, 2016 at 11:18 am Reply | Quote
  • Jonathan Says:

    You saved me hours of research, tnx !

    Posted on May 10th, 2016 at 8:58 pm Reply | Quote
  • Name me b kya rakha hai Says:

    Thank you so so much

    Posted on August 25th, 2016 at 10:16 am Reply | Quote
  • ASH Says:


    Posted on September 21st, 2016 at 7:53 pm Reply | Quote
  • zero Says:

    Thank you sooo much!!

    I’ve been struggeling with their studio and all the sollutions seem to be from people who do it for free out of the community. Not Google… (at least I used their search engine).

    Posted on November 17th, 2016 at 2:24 pm Reply | Quote
  • mosa Says:

    you relley save my life thank you

    Posted on December 4th, 2016 at 2:52 pm Reply | Quote
  • Benjamin Vison Says:

    Incredible how everyone over-complicate things… even in the android development team.. from the beginning of their documentation they start talking about file providers and XYZ.. I loved that you actually kept it pretty simple.. not having to do 30 methods and implementations for such a simple task… of course, everything there is important, but from this base one can simply go further and implement those things such as file provider or generating strings for the images or working on the public directory etc etc… thank you !

    Posted on May 10th, 2017 at 6:05 am Reply | Quote
  • Harish Sharma Says:

    thanks a lot .. you really save my time .. it’s working fine for me.. again thank you

    Posted on September 16th, 2017 at 3:53 pm Reply | Quote

Leave a comment