Android camera photo comes back null

2019-01-29 05:49发布

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?

2条回答
闹够了就滚
2楼-- · 2019-01-29 06:33

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.

       @Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putParcelable("myURI", imageUri);
    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);
    // Restore state members from saved instance
   imageUri = savedInstanceState.getParcelable("myURI");

}
查看更多
Deceive 欺骗
3楼-- · 2019-01-29 06:49

Can anyone explain why this could happen

ACTION_IMAGE_CAPTURE does not return a Uri, according to the documentation. data.getData(), in your code, should always be null. If it is not null, what that Uri means is undocumented.

how to resolve it?

You already know what the Uri is. You put it in EXTRA_OUTPUT. Use that.

I didn't use to have this problem.

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 a Uri, since the documentation does not say that they have to return a Uri (and, even then, there are bugs in camera apps).

查看更多
登录 后发表回答