Android 相机照片返回 null

Android camera photo comes back null

在我的 android 应用程序中,我使用了一个摄像头 activity,有时能用,有时不能用。即使相机图片存储在 phone 上的图片目录中,设置为存储照片的 uri 大约有 30% 的时间返回空值。下面是我的开始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();

下面是我的结果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;
            }

问题是变量 imageUri returns 有时会为 null 每个人,即使照片是拍摄的并且位于 Samsung S4 phone 图片目录中。谁能解释为什么会发生这种情况以及如何解决?如果返回空值,是否有另一种捕获照片的方法?我应该补充一点,我使用的是 Android SDK 版本 23。我以前没有遇到过这个问题。

我有关于这个问题的更多信息。我在 startactivity 中设置我的图像路径如下:

           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);

在我的结果 activity 中,mCurrentPhotoPath 为空。是什么导致这个被消灭?我没有在任何其他会导致它变为空的地方引用 mCurrentPhotoPath。

     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;
                    }
                }

谁能解释为什么 mCurrentPath returns 是一个空值,而我没有在除开始 activity 和结果 activity 以外的任何其他地方引用它?

Can anyone explain why this could happen

根据 the documentation

ACTION_IMAGE_CAPTURE 不 return 一个 Uridata.getData(),在您的代码中,应始终为 null。如果它不是 null,则 Uri 的含义未记录。

how to resolve it?

您已经知道 Uri 是什么了。你把它放在 EXTRA_OUTPUT 中。用那个。

I didn't use to have this problem.

是的,你做到了。有数千种 Android 设备型号。它们附带了数十个(如果不是数百个)内置相机应用程序。还有数十个(如果不是数百个)额外的 ACTION_IMAGE_CAPTURE 支持应用程序可供下载,可以从 Play 商店等地方下载。 None 其中必须 return 一个 Uri,因为文档没有说他们必须 return 一个 Uri(而且,即便如此,也有相机应用程序中的错误)。

感谢 CommonsWare,我通过实现 onSaveInstance State 解决了这个问题。这应该是任何 android 相机文档的必需信息,因为每当您更改相机的方向时都会丢失全局变量。

       @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");

}