1

I'm using the following code (in Kotlin) to select an image from the Google Photos app on my Samsung tablet.

val intent =  Intent (Intent.ACTION_GET_CONTENT)
intent.type = "image/*"
startActivityForResult(intent, REQUEST_GOOGLE_PHOTOS_IMAGE)

I've also tried

Intent (Intent.ACTION_GET_CONTENT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)

On running this code I get a slideout that allows me to select the Photos app, from which I can then select a photo. However, once I select the photo, the app does not return the image to my app as it does with the camera and gallery. It instead returns to the slideout. When I hit the back button to close the slideout, onActivityResult is called with RESULT_CANCELED and 0 data.

I am able to retrieve photos from the Gallery and the camera without issue so I'm not sure what I'm missing. Maybe a permission or something else in the Manifest? Thanks in advance!

jilbot
  • 93
  • 8
  • See my answer [here](https://stackoverflow.com/questions/54957859/pick-from-google-photos-provider-with-action-get-content-or-open-document/64291034#64291034) in case that you start your intent from an activity with `singleInstance` launch mode. that was solve the same issue that you described. – Babak Oct 10 '20 at 07:33

3 Answers3

0

Let try like this,

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        } else {
            intent = new Intent(Intent.ACTION_GET_CONTENT);
        }
Gobu CSG
  • 653
  • 5
  • 7
  • ACTION_OPEN_DOCUMENT doesn't work. In this case, the app skips the slideout entirely (so no option to select Photos or any other app) and goes right to a screen of gallery photos. – jilbot Jul 13 '18 at 17:48
0

Use ACTION_GET_CONTENT Intent Action - Intent.ACTION_GET_CONTENT

Call following selectImage() method to start pick image Intent, which will display all apps available to select image from

private val RC_SELECT_IMGAE = 101

private fun selectImage() {
    val selectImageIntent = Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media
            .EXTERNAL_CONTENT_URI)
    startActivityForResult(selectImageIntent, RC_SELECT_IMGAE)
}

Then handle callback by overriding onActivityResult() method in Activity

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    when (requestCode) {
        RC_SELECT_IMGAE -> {
            if (data != null) {
                val uri = data.data
                displaySelectedImage(getBitmapFromUri(uri))
            }
        }
        else -> super.onActivityResult(requestCode, resultCode, data)
    }
}

Finally update the image view with bitmap as below

private fun getBitmapFromUri(uri: Uri): Bitmap {
    val parcelFileDescriptor = contentResolver.openFileDescriptor(uri, "r")
    val fileDescriptor = parcelFileDescriptor?.fileDescriptor
    val image = BitmapFactory.decodeFileDescriptor(fileDescriptor)
    parcelFileDescriptor.close()
    return image
}

private fun displaySelectedImage(imageBitmap: Bitmap) {
    iv_selected_image.setImageBitmap(imageBitmap)
}

Find Working Image Picker Example here - ImagePickerExample

You may want to add FileProvider in case it gives any kind of URI Exception

adityakamble49
  • 1,971
  • 18
  • 27
  • ACTION_PICK does not work. It allows me to pick from the gallery only, not the google photos app. This is one of the many solutions I had already tried during my search before posting on SO. – jilbot Jul 14 '18 at 14:13
  • I should mention that if I include intent.type = "image/*" in that ACTION_PICK request, I can select a photo from google photos but it returns RESULT_CANCELED to onActivityResult instead of the image. – jilbot Jul 14 '18 at 14:33
  • @jilbot Its working on my side using same code as mentioned above. I have quickly created sample app for picking images. Checkout over here - https://github.com/adityakamble49/image-picker-example – adityakamble49 Jul 14 '18 at 15:05
  • Thanks for taking the time to provide that! In the meantime I have discovered that the problem was that my device was not connected to the network. I turned on WiFi and am now able to get a photo, which has to be downloaded from the cloud. I erroneously expected that my google photos would be stored on the device, but it appears they are in the cloud only. So I have to learn more about the photos app, including if and when it stores my photos on the device. In any case, ACTION_PICK and ACTION_GET_CONTENT now work as expected. – jilbot Jul 14 '18 at 15:28
  • @jilbot Glad to help.You can surely upvote and approve the answer to thank. :) I have updated the post and example with ACTION_GET_CONTENT action. Now its working with offline as well as online files. – adityakamble49 Jul 14 '18 at 15:41
  • Deserves an upvote and I tried to do it earlier but I don't have enough reputation points :-( – jilbot Jul 14 '18 at 16:29
  • @jilbot. No issue. You can accept the answer though :) – adityakamble49 Jul 14 '18 at 17:17
  • I wasn't sure people would see the solution (need to connect to network) all the way down in the comments. I posted it as the answer and accepted it, referring to your helpful answer as well. If/when I get enough rep, I'll add the upvote. – jilbot Jul 15 '18 at 17:41
0

The issue is that I was not connected to the network so Google Photos was unable to retrieve the selected image. See my response to @adityakamble49.

jilbot
  • 93
  • 8