无法将捕获的图像保存到内部存储安卓摄像头

时间:2015-06-22 10:11:00

标签: android camera internal-storage

玩图像,相机和内部存储时遇到问题。 所以我只是想改变我从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)。 因此,我想更改该代码,以便相机活动通过将代码更改为此代码将捕获的图像保存到内部存储:

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

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

enter image description here

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

1 个答案:

答案 0 :(得分:2)

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

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