Android - 相机意图,返回null结果

时间:2014-03-21 11:12:25

标签: android android-intent android-camera

我正在尝试使用下一个代码来从相机中获取图片 -

private void openImageIntent() {

    // Determine Uri of camera image to save.
    final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir" + File.separator);
    root.mkdirs();
    //final String fname = Utils.getUniqueImageFilename();

    long elapsedMsec = System.currentTimeMillis();

     final String fname = "img_"+ System.currentTimeMillis() + ".jpg";
    final File sdImageMainDirectory = new File(root, fname);
    outputFileUri = Uri.fromFile(sdImageMainDirectory);

        // Camera.
        final List<Intent> cameraIntents = new ArrayList<Intent>();
        final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        final PackageManager packageManager = getPackageManager();
        final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
        for(ResolveInfo res : listCam) {
            final String packageName = res.activityInfo.packageName;
            final Intent intent = new Intent(captureIntent);
            intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            intent.setPackage(packageName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            cameraIntents.add(intent);
        }

        // Filesystem.
        final Intent galleryIntent = new Intent();
        galleryIntent.setType("image/*");
        galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

        // Chooser of filesystem options.
        final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");

        // Add the camera options.
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));

        startActivityForResult(chooserIntent, 1);
    }

当Intent完成时,这就是代码 -

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

      if(resultCode == RESULT_OK)
        {
            if(requestCode == 1)
            {
                final boolean isCamera;
                if(data == null)
                {
                    isCamera = true;
                }
                else
                {
                    final String action = data.getAction();
                    if(action == null)
                    {
                        isCamera = false;
                    }
                    else
                    {
                        isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    }
                }

                Uri selectedImageUri;
                if(isCamera)
                {

                    selectedImageUri = outputFileUri;
                    currImageURI = selectedImageUri;

                }
                else
                {
                    selectedImageUri = data == null ? null : data.getData();
                    currImageURI = selectedImageUri;

                }
            }
        }

}

现在我尝试将图像uri保存在一个全局变量中,命名为* currImageURI *

我甚至尝试使用这部分代码,但它似乎破坏了应用程序 -

  if(isCamera)
            {

               currImageURI = data.getData();

            }

我甚至在清单中添加了下一行

        <uses-permission android:name="android.permission.CAMERA" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

现在从画廊中挑选图片一切都很好,但是当我试图从相机中获取图片时,我得到一个空URI。

任何想法为什么?

感谢您提供任何帮助

1 个答案:

答案 0 :(得分:0)

我尝试了这段代码,但遇到了同样的问题

简单的解决方案是像这样向公共变量添加静态类型

private static Uri outputFileUri;

因为打开相机后,活动将关闭,并且链接已从变量中删除