如何避免在相机意图上使用 MediaStore.EXTRA_OUTPUT?

How to avoid using MediaStore.EXTRA_OUTPUT on camera intent?

我正在尝试让我的用户从 phone 图库中挑选照片或使用相机拍照。 我写了一段代码在我的三星 S6 上运行得非常好

private void openPictureIntent() {
        final List<Intent> cameraIntents = new ArrayList<>();
        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);
            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[cameraIntents.size()]));

        startActivityForResult(chooserIntent, SELECT_PICTURE_CODE);
    }

然后我在 Nexus 5X 上试了一下,我的应用程序崩溃了。我了解到在 onActivityResult 中检索到的意图对于某些设备为 null。解决方案是给 uri 在哪里保存用相机拍摄的图像。我实现了它,我的代码变成了

private void openPictureIntent() {
        // Determine Uri of camera image to save.
        final File dir = new File(Environment.getExternalStorageDirectory() + File.separator + "Directory" + File.separator);
        dir.mkdirs();
        final String fname = UUID.randomUUID() + ".jpg";
        final File sdImageMainDirectory = new File(dir, fname);
        outputFileUri = Uri.fromFile(sdImageMainDirectory);
        new File(outputFileUri.getPath()).delete();

        // Camera.
        final List<Intent> cameraIntents = new ArrayList<>();
        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[cameraIntents.size()]));

        startActivityForResult(chooserIntent, SELECT_PICTURE_CODE);
    }

这是可行的,但我不满意。我希望我的照片与其他相机照片一起存储,而不是在特殊目录中。我怎样才能做到这一点?

I learned that the intent retrieved in onActivityResult is null with some devices.

所有相机应用程序都应该是 null。一些相机应用程序开发人员无法阅读 the documentation for ACTION_IMAGE_CAPTURE

I would like my photo to be stored with other camera photo and not in a special directory

ACTION_IMAGE_CAPTURE不支持。