Tomas Černius
Tomas Černius

Reputation: 29

Read picture taken with camera Intent

I'm trying to read and display the picture taken using camera Intent. My code is based on examples found in android docs:

 public void takeSidePhoto(View view) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = createImageFile(REQUEST_IMAGE_CAPTURE_SIDE);
        } catch (IOException ex) {

        }
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this, "my.app.package.fileprovider", photoFile);
            imageSide = photoURI.toString();
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE_SIDE);
        }
    }
}

private File createImageFile(int imageCaptureCode) throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );
    return image;
}

The problem is that the photoURI passed to takePictureIntent is (example): file:/storage/emulated/0/Android/data/my.app.package/files/Pictures/JPEG_20160921_123241_562024049.jpg But when I'm browsing my test device storage with file manager, I can see that the picture that was taken is stored at: file:/storage/Android/data/my.app.package/files/Pictures/JPEG_20160921_123241_562024049.jpg

What is happening and how do I get the path to the real file?

Upvotes: 0

Views: 231

Answers (3)

Imran Ali
Imran Ali

Reputation: 321

Get storage directory using below code and save image to the path.

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();

Upvotes: 0

Madalina Chis
Madalina Chis

Reputation: 51

The takeSidePhoto() method should have following content:

  Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = createImageFile();
                imageUri = Uri.fromFile(photoFile);
            } catch (IOException ex) {
                Toast.makeText(getActivity(), ex.toString(), Toast.LENGTH_SHORT).show();
            }
            if (photoFile != null) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
            }
        } else {
            Toast.makeText(getActivity(), R.string.no_camera_error_message, Toast.LENGTH_SHORT).show();
        }

And in createImageFile() method change this line File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

with this File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

Upvotes: 0

Rahul Khurana
Rahul Khurana

Reputation: 8834

in your try block do something like this to get the path

if(photoFile.exists()){
String path = photoFile.getAbsolutePath()
}

Upvotes: 1

Related Questions