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:
- 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;
- 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.
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.