JungleBook
JungleBook

Reputation: 1

ANDROID - ActivityResultLauncher intent extra return null on ACTION_IMAGE_CAPTURE

I'm trying to add a captured image in the external private storage space of my app.

To do this, I have create a file object with getExternalFilesDir.

    public static File createImageFile(Context context) throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", new Locale("fr_FR")).format(new Date());
        Random rdm = new Random();
        String imageFileName = "JPEG_" + timeStamp + "_" + rdm.nextInt(1000000000) + ".jpg";

        return new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), imageFileName);
    }

After this, I'm getting the URI with FileProvider and the File object created. I send it to pictureAddResult with an intent putExtra. At this time my Uri appears correct.

intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

if(intent.resolveActivity(requireActivity().getPackageManager()) != null) {
   File photoFile;

   try {
      photoFile = ImageUtils.createImageFile(this.requireContext());
   } catch (IOException e) {
      throw new RuntimeException(e);
   }

   Uri photoUri = FileProvider.getUriForFile(this.requireContext(), "com.b2o.rfnd.fileprovider", photoFile);

   intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
   intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
   pictureAddResult.launch(intent);
}

But here, when I trying to get my Uri with getParcelableExtra, it is null all the time.

    ActivityResultLauncher<Intent> pictureAddResult = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        result -> {
            if (result.getResultCode() == Activity.RESULT_OK) {

                Intent intent = result.getData();
                if (intent != null) {
                    // all the time null
                    Uri imageUri = intent.getParcelableExtra(MediaStore.EXTRA_OUTPUT);
                    Log.i("imageuri", String.valueOf(imageUri));
                }
            }
        }
    );

I found a lot of informations when I tryed to found solution on internet but nothing about mine ... In my manifest, I have add the provider

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.b2o.rfnd.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>


   // file path
   <?xml version="1.0" encoding="utf-8"?>
   <paths xmlns:android="http://schemas.android.com/apk/res/android">
      <external-files-path name="images" path="Pictures" />
   </paths>

and the queries

    <queries>
        <intent>
            <action android:name="android.media.action.IMAGE_CAPTURE" />
        </intent>
    </queries>

I think is come from my Provider but I have no idea of what is the problem.

Upvotes: 0

Views: 17

Answers (0)

Related Questions