In my android app I employ a camera activity which sometimes works and sometimes doesn't. About 30% of the time the uri set up to store the photo comes back with a null value even though the camera picture is stored in the picture directory on the phone. Below is my start activity.
Uri imageUri; // These are defined at the global level
Uri selectedImage;
...............
String fileName = name.replace(" ", "");
fileName = fileName + suffix + ".jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION, "Image capture by camera");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
else
Toast.makeText(getApplicationContext(), "Photo was not taken", Toast.LENGTH_SHORT).show();
Below is my result activity.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bitmap image1;
if(requestCode == RESULT_CROP_IMAGE)
{
Bundle extras = data.getExtras();
image1 = extras.getParcelable("data");
}
else {
if (requestCode == RESULT_LOAD_IMAGE) {
selectedImage = imageUri; // returns null occassionally
} else
selectedImage = data.getData();
if(selectedImage == null)
{
Toast.makeText(getApplicationContext(), "Photo not captured correctly", Toast.LENGTH_LONG).show();
return;
}
The problem is that the variable imageUri returns as null everyone once in awhile even though the picture was taken and resides on the Samsung S4 phone picture directory. Can anyone explain why this could happen and how to resolve it? Is there another way to capture the photo if nulls are returned? I should add that I am on Android SDK version 23. I didn't use to have this problem.
I have more information on the problem. I set my imagepath in the startactivity as follows:
String mCurrentPhotoPath; // is stored at the global level
.....
String fileName = name.replace(" ", "");
fileName = fileName + suffix + ".jpg";
mCurrentPhotoPath = fileName;
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, mCurrentPhotoPath);
values.put(MediaStore.Images.Media.DESCRIPTION, "Image capture by camera");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
And in my result activity I get null for mCurrentPhotoPath. What caused this to get wiped out? I did not reference mCurrentPhotoPath in any other place that would cause it to become null.
if (requestCode == RESULT_LOAD_IMAGE) {
if(imageUri != null)
selectedImage = imageUri;
else
{
if(mCurrentPhotoPath != null) { // mCurrentPhotoPath returns null;
File file = new File(Environment.getExternalStorageDirectory().getPath(), mCurrentPhotoPath);
selectedImage = Uri.fromFile(file);
}
else {
Toast.makeText(getApplicationContext(), "Photo not captured", Toast.LENGTH_LONG).show();
return;
}
}
Can anyone explain why mCurrentPath returns a null value when I didn't reference it in any other place other than the start activity and the result activity?
Thanks to CommonsWare I resolved the issue by implementing a onSaveInstance State. This should be required information on any android camera documentation as you lose the global variables whenever you change the camera's orientation.
ACTION_IMAGE_CAPTURE
does not return aUri
, according to the documentation.data.getData()
, in your code, should always benull
. If it is notnull
, what thatUri
means is undocumented.You already know what the
Uri
is. You put it inEXTRA_OUTPUT
. Use that.Yes, you did. There are thousands of Android device models. These ship with dozens, if not hundreds, of camera applications built in. There are dozens, if not hundreds, of additional
ACTION_IMAGE_CAPTURE
-supporting apps available for download, from places like the Play Store. None of them have to return aUri
, since the documentation does not say that they have to return aUri
(and, even then, there are bugs in camera apps).