I’m building an app that takes a photo and then e-mails it. For this I have to save it into the device’s memory. The problem I’m getting is that while the app writes my file, it crashes at this particular line of code:
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(Environment.getExternalStorageDirectory()));
Lower you have the entire function and code:
private void takePicture(){
File outputFile = new File(Environment.getExternalStorageDirectory(), "image.jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(Environment.getExternalStorageDirectory()));
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();
}
}
}
If I comment that line out, the app doesn’t crash anymore. I need to get the photo’s URI so I can then email it.
Any help from you guys more experienced out there would be greatly appreciated.
Try this