0

I am trying to capture photo and need its uri after capturing it. But for some device Uri selectedImage = data.getData(); is returning null. What should i make code change in below code to get this output?

 builder.setPositiveButton(R.string.GALLARY, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(getApplicationContext(), "GALLARY clicked", Toast.LENGTH_LONG).show();
                Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                output=new File(new File(getApplicationContext().getFilesDir(), PHOTOS), FILENAME);
                someActivityResultLauncher.launch(pickPhoto);
            }
        });
   //------when camera is selected to capture photo

    builder.setNegativeButton(R.string.CAMERA, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int j) {

            ///--start this code useful for setting file path available----------
            output=new File(new File(getApplicationContext().getFilesDir(), PHOTOS), FILENAME);
            if (output.exists()) {
                output.delete();
            }
            else {
                output.getParentFile().mkdirs();
            }

            Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            Uri outputUri= FileProvider.getUriForFile(getApplicationContext(), AUTHORITY, output);

            i.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);

            if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
                i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            }
            else if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) {
                ClipData clip=
                        ClipData.newUri(getApplicationContext().getContentResolver(), "A photo", outputUri);

                i.setClipData(clip);
                i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            }
            else {
                List<ResolveInfo> resInfoList=
                        getApplicationContext().getPackageManager()
                                .queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);

                for (ResolveInfo resolveInfo : resInfoList) {
                    String packageName = resolveInfo.activityInfo.packageName;
                    getApplicationContext().grantUriPermission(packageName, outputUri,
                            Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                }
            }

            try {

                Toast.makeText(getApplicationContext(), "CAMERA clicked", Toast.LENGTH_LONG).show();
                someActivityResultLauncher.launch(i);
            }
            catch (ActivityNotFoundException e) {
                Toast.makeText(getApplicationContext(), "nocamera", Toast.LENGTH_LONG).show();
            }
        }
    });

Overide method(onActivityResult)

 ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result.getResultCode() == Activity.RESULT_OK) {

                    Intent i=new Intent(Intent.ACTION_VIEW);
                    Uri outputUri=FileProvider.getUriForFile(getApplicationContext(), AUTHORITY, output);

                    i.setDataAndType(outputUri, "image/jpeg");
                    i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

                    Intent data = result.getData();

                    if(data==null) {  
                       uploadImageFile(outputUri);
                        
                    } else {  

                        Uri selectedImage = data.getData();//------this is null, how to get URI value here?
                       // uploadImageFile(selectedImage);
                       
                    }
                }
            }
        });

I saw few answers like Camera activity returning null android , Android data.getData() returns null from CameraActivity for some phones in Stackoverflow, but i could not understood it. So if you post a answer for issue it will be helpful

Albert
  • 23
  • 1
  • 7
  • You need to handle keeping track of that yourself. It is simply the `Uri` for the `File` that you created for the `ACTION_IMAGE_CAPTURE` above. When you put that `EXTRA_OUTPUT` on the `Intent`, it's assumed that you already know where the file is (obviously), so there is no requirement for the camera app to pass a `Uri` back to you in `onActivityResult()`. – Mike M. Jul 28 '21 at 14:19
  • @MikeM. I read what you are explaining in answer of other stackoverflow question. But i could not understand, can you post as answer to fix my issue. – Albert Jul 28 '21 at 14:25
  • Have a look at [CommonsWare's example here](https://stackoverflow.com/a/45388858), which looks like the basis for most of the code you're using already. Notice how the `Uri` in `onActivityResult()` is recreated from the `private File output` field, instead of trying to pull it from the delivered `Intent`. You should also make note of how that `output` field is saved and restored, since your `Activity` might actually be destroyed while the camera app is running, and then restarted when it's done. – Mike M. Jul 28 '21 at 14:35
  • Upon rereading your `onActivityResult()`, you were most of the way there already, but it looks like some things might've got mixed up in trying to insert the gallery stuff. – Mike M. Jul 28 '21 at 14:42
  • @MikeM. My code is also same as you posted example. Still i am not able to figure it out, how to get Uri when image capture from Camera or selected from Gallary. It will be helpful if you can post answer, it will make me clear. – Albert Jul 28 '21 at 14:50
  • @MikeM. UPdated my question, added code when GALLERY is selected – Albert Jul 28 '21 at 14:54
  • Yeah, seems like things got mixed up when combining the camera and gallery. Setting `output` when launching the gallery doesn't really make sense. You should register separately for the camera and gallery, using the provided [`TakePicture`](https://developer.android.com/reference/androidx/activity/result/contract/ActivityResultContracts.TakePicture) and [`GetContent`](https://developer.android.com/reference/androidx/activity/result/contract/ActivityResultContracts.GetContent) contracts, respectively. Just separating the two should fix the mix-up currently happening in `onActivityResult()`. – Mike M. Jul 28 '21 at 15:33
  • @MikeM. how to register separately for the camera and gallery, using the provided TakePicture and GetContent contracts? Can you show me in code. – Albert Jul 28 '21 at 15:42
  • Basically, you just make two separate `registerForActivityResult()` calls for two different launcher fields; one for the gallery, and a different one for the camera. `GetContent` is demonstrated well in [the docs for the Activity Result API](https://developer.android.com/training/basics/intents/result), and someone has adapted that example into one for `TakePicture` in [this answer here](https://stackoverflow.com/a/65526167). If you need more specific instruction for that, you might have to wait until someone can provide a proper answer below. – Mike M. Jul 28 '21 at 16:01

0 Answers0