GeorgeWChubby
GeorgeWChubby

Reputation: 808

Taking a photo and saving in custom folder, file doesn't work

I'm trying to take a photo with my app and save it in a folder, but for some reason the file just doesn't work. It is created just fine, but it's just an empty file (as far as I can tell).

My code:

private File createImageFile() throws IOException {
    // Create an image file name
    //String timeStamp = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    String imageFileName = "SCB_";
    String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) +
                  File.separator + "/SCBimages/";
    File storageDir = new File(path);
    storageDir.mkdirs();
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    currentPhotoPath = image.getAbsolutePath();
    return image;
}

private void dispatchTakePictureIntent() {

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(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
            System.out.println(ex.getMessage());
            System.out.println(Arrays.toString(ex.getStackTrace()));
            Toast.makeText(getApplicationContext(), "Failed" + ex.getMessage(), Toast.LENGTH_SHORT).show();
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra("", Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
}

Curiously, my phone does take a picture but it is saved in the DCMI folder under another name. Does anyone have an idea as to what the problem is?

Upvotes: 0

Views: 414

Answers (1)

eshayne
eshayne

Reputation: 935

You're missing the MediaStore.EXTRA_OUTPUT extra from your intent. I think the line:

takePictureIntent.putExtra("", Uri.fromFile(photoFile));

should probably be

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));

Upvotes: 1

Related Questions