Camera intent returns small picture

14,396

Solution 1

read this answer to get the full image size How to capture an image and store it with the native Android Camera


when you use reading Bitmap from extra, you will get Thumbnail of the image

Solution 2

I have the same problem, I found an example on the google android site: http://developer.android.com/training/camera/photobasics.html

Solution 3

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:

public static final int CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE = 1777;

  1. Next we fire again the intent to start Activity for result:

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg"); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); startActivityForResult(intent, CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE);

Here we are actually passing an URI as an extra to the intent in order to save the image.

  1. Finally we will receive the result in onActivityResult:

            protected void onActivityResult(int requestCode, int resultCode, Intent data) 
             {  
                 //Check that request code matches ours:
                 if (requestCode == CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE) 
                 {
                     //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);
                 }
        }
    

decodeSampledBitmapFromFile method is:

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) 
{ // BEST QUALITY MATCH

    //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);
}

add the relevent camera permissions to the manifest file:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Hope It would Help anyone how is serching for the solution. As it worked 100% for me.

Share:
14,396
shaylh
Author by

shaylh

Updated on June 04, 2022

Comments

  • shaylh
    shaylh almost 2 years

    I'm using the camera intent to capture a picture. This is my code and it works great:

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    

    my onActivityResult looks like this:

    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap photo = (Bitmap) extras.get("data");
        }
    }
    

    The problem is, that while the picture taken by the camera is 480*800 (I'm using HTC Desire), the bitmap returned is only 194*324!

    Any idea why that happens and how to resolve it?

    Thanks!

  • Kasnady
    Kasnady over 6 years
    i had followed it and i t succeed to store image to gallery. but the image stored also with poor quality image.. how to get good quality?