简体   繁体   English

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

[英]Can't save captured image to Internal storage android camera

I got problem when playing with image, camera, and internal storage. 使用图像,相机和内部存储器时出现问题。 So I just about to change the code that I takes from google which has functionality to save image to sdcard. 所以我只是要更改我从Google那里获取的代码,该代码具有将图像保存到sdcard的功能。 So I changed that code a little bit to this following codes... 所以我将该代码稍微更改为以下代码...

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);
    }
}

As you can see, on createImageFile() method, there is some block code which have commented out, that section makes my code do save the image into external storage (sdcard). 如您所见,在createImageFile()方法上,有一些已注释掉的块代码,该部分使我的代码确实将图​​像保存到外部存储(sdcard)中。 So I want to change that code so that the camera activity save the captured image to internal storage by change that code to this one : 因此,我想更改该代码,以便摄像机活动通过将该代码更改为以下代码,将捕获的图像保存到内部存储中:

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

But when I try to confirm the image capture, the camera activity does not closed, just like in this picture... 但是当我尝试确认图像捕获时,相机活动并没有关闭,就像这张照片一样...

在此处输入图片说明

That "check" icon button does not take any action when being pressed. 该“复选”图标按钮在被按下时不采取任何措施。 So what was happened in here? 那这里发生了什么?

Actually getExternalStoragePublicDirectory is not getting the sdcard, but the primary memory that is shared among apps. 实际上, getExternalStoragePublicDirectory不是获取sdcard,而是获取应用之间共享的主内存。 So you can change it back to getExternalStoragePublicDirectory . 因此,您可以将其更改回getExternalStoragePublicDirectory

And also don't forget to change localStorage variable in commented code 并且也不要忘记在注释的代码中更改localStorage变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM