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

3
我尝试使用以下代码从相机获取图片 -
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
是的...你只会收到null。你捕获的图像将保存在你通过键MediaStore.EXTRA_OUTPUT传递的额外参数指定的位置... - Gopal Gopi
希望以下链接对您有用:https://dev59.com/VWkw5IYBdhLWcg3wbqKZ https://dev59.com/U2w05IYBdhLWcg3w3ViX - Farhan Shah
你是否已经添加了访问外部存储的权限(WRITE_EXTERNAL_STORAGE)? - Vijju
但是看起来[outputFileUri]正在通过意图传递,并在[onActivityResult]中被重复使用。是的,我已经使用了- WRITE_EXTERNAL_STORAGE。 - 4this
1个回答

0

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

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

private static Uri outputFileUri;

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接