从Camera获取图像路径有时会在Android中返回null

时间:2014-11-25 17:34:29

标签: android android-camera-intent

我正在用手机拍照(在三星和nexus 4上测试),然后获取图像路径。很奇怪,在三星上它可以工作,在nexus 4上它返回null。相同的代码..

// Get image from captured image
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
 startActivityForResult(takePictureIntent, ACTION_REQUEST_CAMERA);
}

onActivityResult:

       case ACTION_REQUEST_CAMERA:

            if (requestCode == ACTION_REQUEST_CAMERA && resultCode == RESULT_OK) {
                photoTakenFromCamera = true;
                if (intent != null) {
                    Bundle extras = intent.getExtras();
                    Bitmap imageBitmap = (Bitmap) extras.get("data");
                    takenImg.setImageBitmap(imageBitmap);
                    targetUri = intent.getData();
                } else {
                    System.out.println("intent is null. don't know why.");
                    targetUri = takenImagePath;
                }

                if (targetUri != null) {
                    System.out.println("TargetUri path: " + targetUri.getPath());
                } else {
                    System.out.println("targetUri is null.");
                }
            }
            break;

在nexus手机上,targetUri返回null。

minSdkVersion是15,目标是19.有没有人看到我在这里缺少什么?谢谢。

1 个答案:

答案 0 :(得分:0)

您应该使用此库(https://github.com/ralfgehrer/AndroidCameraUtil)。有些设备有不同的实现......

这个库准备处理这些差异