【问题标题】:Can't save captured image to Internal storage android camera无法将捕获的图像保存到内部存储 android 相机
【发布时间】:2015-09-07 17:56:32
【问题描述】:

我在使用图像、相机和内部存储时遇到问题。 因此,我正要更改从谷歌获取的代码,该代码具有将图像保存到 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();
}

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

“检查”图标按钮在被按下时不会采取任何行动。 那么这里发生了什么?

【问题讨论】:

  • 你在哪里处理 onActivityResult..
  • 将 getFilesDir 更改为 getExternalFilesDir。
  • 我以为我不需要onActivityResult,因为当我使用注释代码时,相机活动已关闭,但只有未保存的图像。不过我会试试的
  • 只有当我有 SD 卡时 getExternalFilesDir 才有效?在这种情况下,我没有 SD 卡,也不想使用 SD 卡

标签: android camera internal-storage


【解决方案1】:

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

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

【讨论】:

  • 我实际上需要在 onActivityResult() 调用画廊。然后一切运行良好。
猜你喜欢
  • 2017-12-11
  • 1970-01-01
  • 2013-10-28
  • 2015-10-19
  • 1970-01-01
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多