Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android IMAGE_CAPTURE returns RESULT_CANCELED

I'm trying to use MediaStore.ACTION_IMAGE_CAPTURE intent to take a profile photo.

The code that starts the intent:

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File myPicture = new File(OfflineApp.getAppContext().getFilesDir() + "/" + getResources().getString(R.string.contact_photos_dir), getResources().getString(R.string.my_photo_file_name));
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(myPicture));
startActivityForResult(cameraIntent, REQUEST_CODE_TAKE_PHOTO);

And the onActivityResult part:

case REQUEST_CODE_TAKE_PHOTO:
    if (resultCode == RESULT_OK) {
        String path = new File(OfflineApp.getAppContext().getFilesDir() + "/" + getResources().getString(R.string.contact_photos_dir), getResources().getString(R.string.my_photo_file_name)).getAbsolutePath();
        ContactManager.getInstance().updateMyImage(path);
    }
    break;

However, the result code is always RESULT_CANCELED, only if I provide MediaStore.EXTRA_OUTPUT, even if the file doesn't exist.

like image 202
Eitan F Avatar asked Jan 08 '23 05:01

Eitan F


2 Answers

Okay. Found the problem.

Since Camera Intent is launching the Camera app, it has no access to my apps file folder. Fixed it by passing an external folder file and copying it to my local folder.

Cheers.

like image 134
Eitan F Avatar answered Jan 09 '23 20:01

Eitan F


You can let the camera app save the image in an external file like this, then use the image from there later

private void captureImage() {
    // You can pass an external file Uri to the intent to which the camera app can write
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // File file = new File(getCacheDir(), IMAGE_FILENAME); // this doesn't work as the camera app doesn't have permission to this apps cache dir
    // File file = new File("/sdcard/image.jpg"); // avoid hardcoding file names
    File file = new File(Environment.getExternalStorageDirectory(), IMAGE_FILENAME);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));

    // intent.setData(Uri.fromFile(file));
    // intent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, REQUEST_CODE_CAPTURE_IMAGE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK){
        if(requestCode == REQUEST_CODE_CAPTURE_IMAGE){
            File file = new File(Environment.getExternalStorageDirectory(), IMAGE_FILENAME);
            Uri uri = Uri.fromFile(file);
            mImagePath = uri.getPath();
            String extension = MimeTypeMap.getFileExtensionFromUrl(mImagePath);
            if(extension != null){
                mImageMIMEType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
            } else {
                mImageMIMEType = "image/jpg"; // fallback, also a best guess
            }
            // use image here
        }
    }
}
like image 25
John Avatar answered Jan 09 '23 18:01

John