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

时间:2017-04-12 11:53:24

标签: android android-intent android-camera

我试图允许我的用户从手机图库中挑选照片或用相机拍照。 我写了一段在我的三星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中检索到的intent在某些设备中为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);
    }

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

1 个答案:

答案 0 :(得分:2)

  

我了解到onActivityResult中检索到的意图对于某些设备是空的。

对于所有相机应用,它应该是null。一些相机应用开发者无法阅读the documentation for ACTION_IMAGE_CAPTURE

  

我希望我的照片与其他相机照片一起存储,而不是存放在特殊目录中

ACTION_IMAGE_CAPTURE不支持。