Can't save captured image to Internal storage android camera

3.3k views Asked by At

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. 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). 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...

enter image description here

That "check" icon button does not take any action when being pressed. So what was happened in here?

1

There are 1 answers

1
euclid135 On BEST ANSWER

Actually getExternalStoragePublicDirectoryis not getting the sdcard, but the primary memory that is shared among apps. So you can change it back to getExternalStoragePublicDirectory.

And also don't forget to change localStorage variable in commented code