0

I searched several answer in stackoverflow but i could not find it so posting this question.

Below code opens the camera and when we take picture and select it using (tick mark) below second code get called but Uri selectedImage = data.getData(); shows as null, why? how to fix this issue so i get proper value for selectedImage!

When Camera option selected, this code executes

                 Uri uri = FileProvider.
                 getUriForFile(getContext(),
                 BuildConfig.APPLICATION_ID + ".provider",
                 new File(Environment.getExternalStorageDirectory(),
                                        "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
                Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                takePicture.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
                takePicture.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri); 
                takePicture.putExtra("return-data", true);


                Toast.makeText(getContext(), "CAMERA clicked", Toast.LENGTH_LONG).show();
                someActivityResultLauncher.launch(takePicture);

And above code calls this

 ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    if (result.getResultCode() == Activity.RESULT_OK) {
                        // There are no request codes
                        Intent data = result.getData();
                        Uri selectedImage = data.getData(); //<----this is showing NULL, why?
                        uploadImageFile(selectedImage);
                    }
                }
            });
  • There is no returned `Uri`. The image should be written to the location identified by the `uri` value that you are supplying via `EXTRA_OUTPUT`. FWIW, [this sample app](https://github.com/commonsguy/cw-omnibus/tree/v8.6/Camera/FileProvider) shows this in action, though it uses an older version of `FileProvider`. That sample is covered in [this free book](https://commonsware.com/Android/Android-8.6-CC.pdf). – CommonsWare Jul 17 '21 at 21:44
  • @CommonsWare I did not understand this `There is no returned Uri. The image should be written to the location identified by the uri value that you are supplying via EXTRA_OUTPUT` can you post answer with code please – Ashutosh Singh Jul 17 '21 at 21:56
  • I linked to three duplicate questions, a complete sample project, and a free book, which has an entire chapter devoted to taking pictures using the camera. I feel reasonably confident that, in 12 minutes, you did not look at those very extensively. You are telling the third-party camera app, in `EXTRA_OUTPUT`, where it should save the photo. Your value for `EXTRA_OUTPUT` is based on `new File(Environment.getExternalStorageDirectory(), "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"))`. So, go look there for your photo. – CommonsWare Jul 17 '21 at 22:01
  • @CommonsWare I tried the example from sample project, i get exception `java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/app.traffic.c.challan/files/photos/CameraContentDemo.jpeg` – Ashutosh Singh Jul 17 '21 at 22:18
  • Your `FileProvider` configuration does not cover `getFiles()` on `Context`. Make sure that your `FileProvider` metadata contains a `` element. – CommonsWare Jul 17 '21 at 22:33

0 Answers0