无法将捕获的图像保存到内部存储器 android 相机

Can't save captured image to Internal storage android camera

我在使用图像、相机和内部存储时遇到问题。 因此,我正要更改从 google 获取的代码,该代码具有将图像保存到 SD 卡的功能。所以我将该代码稍微更改为以下代码...

public class CameraUtility {
    private String currentPhotoPath;
    private String imageCategoryName;

    private ActionBarActivity activity;

    private static final int REQUEST_TAKE_PHOTO = 1;

    public CameraUtility(String imageCategoryName, ActionBarActivity activity){
        this.imageCategoryName  = imageCategoryName;
        this.activity           = activity;
    }

    public String getCurrentPhotoPath(){
        return currentPhotoPath;
    }

    public String getImageCategoryName(){
        return imageCategoryName;
    }

    public File createImageFile() throws IOException{
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = imageCategoryName + "_" + timeStamp + "_";

        File localStorage = activity.getApplicationContext().getFilesDir();
        /*
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  // prefix
                ".jpg",         // suffix
                localStorage    // directory
        );
        */
        File image = new File(localStorage, imageFileName.concat(".jpg"));
        if(!image.exists()){
            image.createNewFile();
        }
        else{
            image.delete();
            image.createNewFile();
        }

        Log.d("LOCAL_STORAGE", localStorage.getAbsolutePath());

        // Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = "file:" + image.getAbsolutePath();
        return image;
    }

    public void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                Toast.makeText(activity.getApplicationContext(),
                        "Error occurred while creating the File",
                        Toast.LENGTH_SHORT).show();

            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Log.d("CAPTURED_AT", currentPhotoPath);
                Toast.makeText(activity.getApplication().getApplicationContext(),
                        "Start Taking the Picture",
                        Toast.LENGTH_SHORT).show();
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photoFile));
                activity.startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
            else{
                Toast.makeText(activity.getApplication().getApplicationContext(),
                        "File was not successfully created",
                        Toast.LENGTH_SHORT).show();
            }
        }
        else{
            Toast.makeText(activity.getApplication().getApplicationContext(),
                    "Activity package manager is null",
                    Toast.LENGTH_SHORT).show();
        }
    }

    public void galleryAddPic() {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(currentPhotoPath);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        activity.sendBroadcast(mediaScanIntent);
    }
}

如您所见,在 createImageFile() 方法上,有一些块代码已被注释掉,该部分使我的代码将图像保存到外部存储(sdcard)中。 所以我想更改该代码,以便相机 activity 通过将该代码更改为以下代码将捕获的图像保存到内部存储器:

File image = new File(localStorage, imageFileName.concat(".jpg"));
if(!image.exists()){
    image.createNewFile();
}
else{
    image.delete();
    image.createNewFile();
}

但是当我尝试确认图像捕获时,相机 activity 没有关闭,就像在这张照片中...

"check" 图标按钮在按下时不执行任何操作。 那么这里发生了什么?

其实getExternalStoragePublicDirectory获取的不是sdcard,而是应用之间共享的主内存。所以你可以把它改回 getExternalStoragePublicDirectory.

并且不要忘记在注释代码中更改 localStorage 变量