I’m working on an app that, among other things, uses the device’s camera to take a photo and then share it through email.
The problem I’m having is that I can’t get the app to take the full sized picture. It always sends a reduced in resolution version of the photo, although the camera is set to 5MP and quality when compressing is set to 100. Below you have my code:
private void takePicture(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == CAMERA_PIC_REQUEST && resultCode == Activity.RESULT_OK){
picture = (Bitmap) data.getExtras().get("data");
pictureView.setImageBitmap(picture);
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "Picture");
values.put(Images.Media.BUCKET_ID, "picture_ID");
values.put(Images.Media.DESCRIPTION, "");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
pictureUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
OutputStream outstream;
try{
outstream = getContentResolver().openOutputStream(pictureUri);
picture.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
outstream.close();
}catch(FileNotFoundException e){
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
…..
share.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, selectedType);
intent.putExtra(Intent.EXTRA_TEXT,Notes + "\nLocation: " + selectedLocation+"\nOwner: " + selectedOwner
+ "\nStatus: " + selectedStatus);
intent.putExtra(Intent.EXTRA_STREAM, pictureUri);
try{
startActivity(Intent.createChooser(intent, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(), "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
});
There is an extra for the
ACTION_IMAGE_CAPTUREintent with the keyMediaStore.EXTRA_OUTPUTwhich takes an URI to a file as the value. If you don’t supply this extra, a size-reduced version of the taken image is returned toonActivityResult()with the data intent.The reason for this is that a full-sized camera picture is simply too big for the intent system to handle (it might work in theory, but slows down the whole intent processing a lot – intents should be as small as possible in general). So it can’t be delivered like the small-size version.
To use this extra modify your
takePicture()method, e.g. like this:This does work like your method above, with the exception that the camera app has written a full-size copy of the image into the file you specified when
onActivityResult()is called.This means you don’t have to write the image to the disk on your own, just open it from there when your
onClickListener()is executed like you did already.