Android相机照片返回null [英] Android camera photo comes back null

查看:206
本文介绍了Android相机照片返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的android应用中,我采用了有时无法正常工作的相机活动.即使相机照片存储在手机的图片目录中,大约30%的uri设置用于存储照片的uri也会返回空值.以下是我的开始活动.

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

下面是我的结果活动.

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

问题在于,即使已拍摄照片并将其驻留在Samsung S4手机照片目录中,变量imageUri也会偶尔一次将所有人返回null.谁能解释为什么会发生这种情况以及如何解决呢?如果返回空值,还有另一种捕获照片的方法吗?我应该补充一点,我使用的是Android SDK版本23.我以前从来没有遇到这个问题.

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.

我有关于此问题的更多信息.我在startactivity中设置图像路径,如下所示:

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

在我的结果活动中,我的mCurrentPhotoPath为空.是什么原因导致这种情况消失了?我没有在任何其他地方引用过mCurrentPhotoPath,这会导致它变为null.

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

有人能解释为什么当我在开始活动和结果活动之外的其他地方未引用mCurrentPath时,它会返回空值吗?

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?

推荐答案

任何人都可以解释为什么会发生这种情况

Can anyone explain why this could happen

根据文档.在您的代码中,data.getData()应该始终为null.如果不是null,则Uri的含义没有记载.

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?

您已经知道Uri是什么.您将其放在EXTRA_OUTPUT中.用那个.

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

我以前不曾遇到这个问题.

I didn't use to have this problem.

是的,你做到了.有数千种Android设备型号.这些附带了数十个(甚至不是数百个)内置的相机应用程序.还有数十个(甚至不是数百个)其他支持ACTION_IMAGE_CAPTURE的应用程序可以从Play商店下载.他们没有一个必须返回Uri,因为文档没有说他们必须返回Uri(即使那样,相机应用程序中也有错误).

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

这篇关于Android相机照片返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆