I am developing an Android App which uploads an image from the camera or from the device photo gallery to remote site. The latter I have working fine, I can select and upload. However, I am have trouble taking a full size image and uploading. This is my code:
// From onCreate
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
I have a method to handle the Activity result. This both handles selecting from the gallery and from the camera:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
String filepath = "";
Uri selectedImageUri;
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_PIC_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
// Gets real path of image so it can be uploaded
selectedImageUri = getImageUri(getApplicationContext(), photo);
}
else {
selectedImageUri = data.getData();
}
// Handle the upload ...
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
This works however, the image is small and does not follow the naming convention for stored images. I have read that to save full size I need to use putExtra and pass MediaStore.EXTRA_OUTPUT on the cameraIntent and declare a temporary file, so I have adapted my intent code as follows:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
camImgFilename = sdf.format(new Date())+".jpg";
File photo = new File Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), camImgFilename);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
This however results in an error being thrown stating "Source not found".
Not sure where to proceed from here?
UPDATE
It appears that my photo is being created. Once the app has closed I can navigate with the file explorer to the directory and see the image. I'm unsure of what is causing the app to crash.
I think the problem is that you are still trying to access the thumbnail from the
Intent
. In order to retrieve the full size image you need to access the file directly. So I'd recommend saving the file name before starting the camera activity and then loading the file atonActivityResult
.I suggest reading Taking Photos Simply page from the official documentation where you'll find this quote regarding the thumbnail:
Also in the very last section, you'll find the code you need: