android - 将相机拍照 Intent 中的图像保存到应用程序私有(private)存储

标签 android android-intent android-camera android-camera-intent

这里有很多关于如何在 android 的外部存储上创建文件,将 uri 传递给 ACTION_IMAGE_CAPTURE 然后将图像保存在那里的信息。

但是,我想在应用程序的私有(private)存储(有时称为内部存储)中创建一个文件并将该文件 uri 传递给 ACTION_IMAGE_CAPTURE,但相机 Intent 在 onActivityResult 中返回 RESULT_CANCELED。

我该怎么办?

private void checkWhetherCameraIsAvailableAndTakeAPicture() {
    // Check wether this device is able to take pictures
    if (PhotoHandler.isIntentAvailable(a, MediaStore.ACTION_IMAGE_CAPTURE)) {
        System.gc();
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File imageFile = null;

        try {
                if(a.application.user.isPublishImagesToGallery()){
                imageFile = a.application.photoHandler.createImageFileOnExternalStorage();
            } else {
                imageFile = a.application.photoHandler.createImageFileOnInternalStorage();
            }

            imagePath = imageFile.getAbsolutePath();
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
        } catch (IOException e) {
            e.printStackTrace();
            imageFile = null;
            imagePath = null;
        }
        startActivityForResult(takePictureIntent, Preferences.REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA);
    } else { // Notify the user that their device is unable to take photos
        a.application.toastMaker.toastLong(ErrorMessages.DEVICE_UNABLE_TO_TAKE_PHOTOS);
    }
} // End of checkWhetherCameraIsAvailableAndTakeAPicture

public File createImageFileOnInternalStorage() throws IOException {
        return createTempFileOnGivenLocation();
    }

    private String imageFilename() {
        return filenamePrefix + Calendar.getInstance().getTimeInMillis();
    }

    private File createTempFileOnGivenLocation() throws IOException {
        File imageFile = File.createTempFile(imageFilename(), filenameSuffix, context.getFilesDir());
        setImagePathTemporary(imageFile.toString());

        return imageFile;
    }


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == Preferences.REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA) {
        if (resultCode != Activity.RESULT_CANCELED) {
            handlePhotoTakenWithCamera();
        } else {
            // ONACTIVITYRESULT Returns Here!
            new File(imagePath).delete())
        }
    } else if (requestCode == Preferences.REQUEST_CODE_PICK_IMAGES_FROM_GALLERY) {
        if (resultCode != Activity.RESULT_CANCELED) {
            handlePhotosPickedFromGallery(data);
        }
    }
} // End of onActivityResult

最佳答案

每个应用程序的内部存储空间都是私有(private)的。 Camera App 无法访问您的应用程序的内部存储。在内部存储中获取图像的最佳方式是。

1) 在应用内使用您自己的相机拍照

2) Take image 使用 camera intent 拍摄图片,将图片保存到外部存储。 onActivityResult 将图像复制到您应用的内部存储并从外部存储中删除。可以在此答案中找到复制文件的代码。 https://stackoverflow.com/a/4770586/4811470

关于android - 将相机拍照 Intent 中的图像保存到应用程序私有(private)存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30726264/

相关文章:

android - Android 中的 Intent 是什么?

java - 我怎样才能找到所有具有特定 Intent 过滤器的包?

java - 使用Intent打开本地PDF文件

android - Android Studio不起作用

android - 在 xamarin 表单的标签页(Android)中插入图标

java - Android相机点击连续照片

java - 如何设置 cwac-camera preview 以使用 scaleType centerCrop?

Android - 使相机预览半透明

java - Firebase 未初始化

java - 返回当前的 android 布局或屏幕宽度以进行调试