Can';t将捕获的图像保存到内部存储器android摄像头

Can';t将捕获的图像保存到内部存储器android摄像头,android,camera,internal-storage,Android,Camera,Internal Storage,我在玩图像、相机和内部存储时遇到问题。 所以我只是想改变我从谷歌那里得到的代码,它有将图像保存到SD卡的功能。所以我把那个代码改成了下面的代码 public class CameraUtility { private String currentPhotoPath; private String imageCategoryName; private ActionBarActivity activity; private static final int REQ

我在玩图像、相机和内部存储时遇到问题。 所以我只是想改变我从谷歌那里得到的代码,它有将图像保存到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()方法中,有一些块代码已经注释掉了,该部分使我的代码将图像保存到外部存储器(SD卡)中。 因此,我想更改该代码,以便相机活动通过将该代码更改为以下代码将捕获的图像保存到内部存储器:

File image = new File(localStorage, imageFileName.concat(".jpg"));
if(!image.exists()){
    image.createNewFile();
}
else{
    image.delete();
    image.createNewFile();
}
但是当我尝试确认图像捕获时,相机活动并没有关闭,就像在这张照片中

按下“check”(检查)图标按钮时不会执行任何操作。
那么这里发生了什么呢?

实际上
getExternalStoragePublicDirectory
并没有获取SD卡,而是在应用程序之间共享的主内存。因此,您可以将其更改回
getExternalStoragePublicDirectory


另外,不要忘记更改注释代码中的localStorage变量,您正在处理onActivityResult。.将GetFileDir更改为GetExternalFileDir。我想我不需要onActivityResult,因为在使用注释代码时,相机活动已关闭,但只有未保存的图像。但我会让它试试只有当我有SD卡时getExternalFilesDir才能工作?在这种情况下,我没有SD卡,也不想使用SD卡。实际上,我需要在onActivityResult()上调用gallery。然后一切都很顺利。