Android 相机照片返回 null

标签 android android-camera-intent

在我的 Android 应用程序中,我使用了有时有效有时无效的相机 Activity 。大约 30% 的时间,即使相机图片存储在手机的图片目录中,uri 设置为存储照片也会返回空值。下面是我的开始 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 偶尔返回 null everyone 即使照片是拍摄的并且驻留在 Samsung S4 手机图片目录中。谁能解释为什么会发生这种情况以及如何解决?如果返回空值,是否有另一种捕获照片的方法?我应该补充一点,我使用的是 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 在除开始 Activity 和结果 Activity 之外的任何其他地方都没有引用它时返回空值?

最佳答案

Can anyone explain why this could happen

ACTION_IMAGE_CAPTURE 不返回 Uri,根据 the documentation . data.getData(),在您的代码中,应始终为 null。如果它不是 null,则 Uri 的含义是未记录的。

how to resolve it?

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

I didn't use to have this problem.

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

关于Android 相机照片返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34119483/

相关文章:

android - 从图库中抓取图片时出现空指针异常

java - float 大于或小于零

android - java.lang.OutOfMemoryError处理来自Camera的14个数据

android - 最佳实践 - 将您的应用添加到 Android 共享菜单

android - Fragment 中的 ViewPager 在返回时丢失内容

android - 无法在 Android 的 Camera Intent 上保存全尺寸照片

android - 如何压缩图片大小?

android - Android 中的相机 Intent 和优先级队列

android - 使用自定义textview的字体图标的可访问性

android - APK 压缩级别,尺寸减小