'take picture just in Android 12 the result code is zero

I try to take the picture Uri in the onActivityResult() and i'm get resultCode == 0 only on Android 12. on all the other devices I tested it works fine.

this is the code to start gallery and camera

    final File root = new File(Environment.getExternalStorageDirectory() + 
    File.separator + "HofitApp" + File.separator);
    root.mkdirs();
    final String fname = "AddProductImage.jpg";
    final File sdImageMainDirectory = new File(root, fname);

    //      mProductImageUri = Uri.fromFile(sdImageMainDirectory);
    mProductImageUri = FileProvider.getUriForFile(this,
            BuildConfig.APPLICATION_ID + ".provider", sdImageMainDirectory);

    final List<Intent> cameraIntents = new ArrayList<Intent>();
    final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    final PackageManager packageManager = getPackageManager();
    final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for (ResolveInfo res : listCam) {
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(packageName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mProductImageUri);
        cameraIntents.add(intent);
    }

    // Filesystem.
    final Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

    // Chooser of filesystem options.
    final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");

    // Add the camera options.
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));

    startActivityForResult(chooserIntent, EXT_PICK_IMAGE_REQUEST_CODE);

Has anyone have this problem and know how to fix it?

I have tried all kind of ways, i try to change from Uri.fromFile(sdImageMainDirectory) to FileProvider and still not working properly, result code is zero after taking a picture.

thanks



Solution 1:[1]

Replace your Temp Image Location -

 final File root = new File(Environment.getExternalStorageDirectory() + 
 File.separator + "HofitApp" + File.separator);

to

final File root =  context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)?.path+File.separator+"HofitApp"+File.separator;

and camera capture intent go like that -

val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri)

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
    cameraIntent.clipData = ClipData.newRawUri("", uri)
    cameraIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION)
}

startActivityForResult(cameraIntent, IMAGE_CAPTURE_REQUEST)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Harsh Mantri