4

I have an published app that uses an ACTION_GET_CONTENT intent to let the user select one of his/her pictures. Here's the code that launches the intent:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
intent.putExtra(Intent.CATEGORY_OPENABLE, true);
startActivityForResult(Intent.createChooser(intent, getString(R.string.select_pic)), SELECT_IMAGE);

And here's the onActivityResult:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(resultCode == RESULT_OK) {
        if (requestCode == SELECT_IMAGE) {
            if (data != null) {
                Uri uri = data.getData();
                ...
                }
            }
        }
    }

    super.onActivityResult(requestCode, resultCode, data);
}

The problem here is that, on some devices, uri is null even though the user has selected an image. Mostly Samsung devices and also some Motorola phones. I also tried with ACTION_PICK instead of ACTION_CONTENT, but it happenned too in some other devices. So I would like to know if there's a way to let the user pick a picture that is universal.

Some phones that suffer this issue are:

  • Samsung galaxy A21S - Android 11 (SDK 30)
  • Samsung galaxy A3 (2016) - Android 7.0 (SDK 24)
  • Motorola moto e6 play - Android 9 (SDK 28)

Some of the users of these affected devices are notifying that it happens only sometimes.

Josemafuen
  • 682
  • 2
  • 16
  • 41
  • You have to try `ACTION_OPEN_DOCUMENT` too. – blackapps Nov 22 '21 at 15:30
  • 1
    I have already added it. About ACTION_OPEN_DOCUMENT, I don't really like it, because the picker interface has less options. Anyways, do you have any justification to choose ACTION_OPEN_DOCUMENT? – Josemafuen Nov 22 '21 at 15:45
  • Hopefully [this](https://stackoverflow.com/questions/37698471/image-uri-doesnt-display-images-on-imageview-on-some-android-device) could help you out – Zain Nov 26 '21 at 21:08

3 Answers3

0

Try this solution

val getImage = registerForActivityResult(ActivityResultContracts.GetContent()) {
        if (it != null) {
            binding.image.setImageURI(it)
        }
    }
    binding.image.setOnClickListener {
        getImage.launch("image/*")
    }
ANDROID
  • 54
  • 12
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 29 '21 at 19:04
0

For me the activity launchMode was set to singleInstance due to which image uri when picked from gallery was returned as null changing it did the trick.

-1

OnActivityResult deprecated

Try the new way

ActivityResultLauncher<String> launcher;

.....

Button pick = findViewById(R.id.pick);
pick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                launcher.launch("image/*");
            }
        });

launcher = registerForActivityResult(new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>() {
        @Override
        public void onActivityResult(Uri result) {
            
        }
    });

}
droid24
  • 124
  • 6
  • Could you link the documentation that says this. I'll test it anyway. – Josemafuen Nov 22 '21 at 15:53
  • @JOSEMAFUEN check https://developer.android.com/training/basics/intents/result#java or https://stackoverflow.com/questions/62671106/onactivityresult-method-is-deprecated-what-is-the-alternative – droid24 Nov 22 '21 at 16:13
  • Thanks, this is a great point and I'll definitelly test it. Anyways, do you have any hint about if this way it will work for all devices. Or is it just "try this way to see if it gets better"? – Josemafuen Nov 22 '21 at 16:14
  • @JOSEMAFUEN I have tried it on several devices in the past and it was working fine, also Google says in the documentation that the activity may be destroyed for several reasons including low memory or other so it will return with a null value https://developer.android.com/training/basics/intents/result#register – droid24 Nov 22 '21 at 16:31
  • 1
    Sadly, this solution didn't work out. When the app opens the file explorer for me to choose, if I choose one "recent" picture, it works. But if I open my Gallery from the file explorer and select a picture, the uri returned is null. – Josemafuen Nov 22 '21 at 19:23