1

I was experiencing some weird behavior in Android. I'm trying to capture an image and it works on Android 5.0 and above including Android 7.0. Here are the links I followed - Link1 Link2

The weird behavior happens on Android 4.4.2 (kitkat). It allows me to click a picture and I click "Okay" and The resultCode in onActivityResult is always 0 (RESULT_CANCELED). But it properly returns -1 (RESULT_OK) on lollipop and above. I mean I clicked Okay, why is it returning RESULT_CANCELED.

What could be wrong here ?

Here is some code:

private String mCurrentPhotoPath;

private void dispatchTakePictureIntent() throws IOException {
    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
            return;
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(ImageCropActivity.this,
                    "myFileProviderName",
                    photoFile);

            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQ_CODE_PERMISSIONS_CAPTURE);
        }
    } else {
        Log.e(TAG, "Could not start camera. Intent(MediaStore.ACTION_IMAGE_CAPTURE) could not be resolved");
        //handle failure here
    }
}



private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DCIM), "Camera");
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

//previously - mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    mCurrentPhotoPath = image.getAbsolutePath();

    return image;
}




@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQ_CODE_PERMISSIONS_CAPTURE) {
        if(resultCode == RESULT_OK) {
            Log.i(TAG, "Picture taken");
            // Show the thumbnail on ImageView
            Uri imageUri = Uri.parse(mCurrentPhotoPath);
            mImageUri = imageUri;
            initImage();

            // ScanFile so it will be appeared on Gallery
            MediaScannerConnection.scanFile(ImageCropActivity.this,
                    new String[]{imageUri.getPath()}, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        public void onScanCompleted(String path, Uri uri) {
                        }
                    });
        }
        else if(resultCode == RESULT_CANCELED){
            Log.i(TAG, "Picture not taken");
           finish();
        }
        else {
            errored();
        }
    }
}

I can provide more code or more info if needed.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
karthik prasad
  • 738
  • 7
  • 15
  • https://commonsware.com/blog/2016/08/31/granting-permissions-uri-intent-extra.html – CommonsWare Mar 16 '17 at 15:28
  • Maybe there is not enough memory on your KitKat device? Android manages such situations by simply closing your app. After that, when the Camera app fulfills the intent, the system creates your activity again. There may be weird side-effects, e.g. the activity is created in landscape, destroyed, and recreated in portrait. – Alex Cohn Mar 16 '17 at 15:29
  • All these photos are clicked and I can actually see them in the Gallery app. – karthik prasad Mar 16 '17 at 18:02
  • I'm experiencing a similar behavior with a custom intent. Did you make any progress on this? – Thomas Jun 29 '17 at 06:16

1 Answers1

2

I resolved my problem by doing the following changes...

private void takePictureFromCamera() {
    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;
        photoFile = createImageFile();
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(activity,
                    "com.example.provider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);

            //COMPATIBILITY
            if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.LOLLIPOP) {
                takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            } else {
                List<ResolveInfo> resInfoList = activity.getPackageManager().queryIntentActivities(takePictureIntent, PackageManager.MATCH_DEFAULT_ONLY);
                for (ResolveInfo resolveInfo : resInfoList) {
                    String packageName = resolveInfo.activityInfo.packageName;
                    activity.grantUriPermission(packageName, photoURI, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                }
            }
            //COMPATIBILITY
            startActivityForResult(takePictureIntent, CAMERA);
        }


    }
}

I found the solution in:

link

ja12
  • 351
  • 2
  • 16