Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – Save image path from gallery to Room DB, then load with Picasso not working

Posted on November 14, 2022

Solution 1 :

Just in case anyone else has this problem, here’s my solution:

  1. Change the intent from ACTION_GET_CONTENT to ACTION_OPEN_DOCUMENT

  2. Set the following flags for the intent:
    chooseFile.flags = (Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)

  3. In onActivityResult use the following line of code before using the URI for anything else:
    context?.contentResolver?.takePersistableUriPermission(returnUri, Intent.FLAG_GRANT_READ_URI_PERMISSION)

Problem :

I’ve tried various solutions on SO for this problem, but so far not one of them has worked :/

I’m letting users pick a photo from a gallery, have the app display a preview, save the image with a caption to a Room DB and then display multiple images in a RecyclerView using Google’s Material Design Card Views.

Picking an image and showing it in the preview is not a problem. But displaying the images in the RecyclerView using Picasso is not working. It only shows my error image (broken image vector).

The following code is used to pick an image from the gallery and display the preview image:

private fun choosePicture() {
    var chooseFile = Intent(Intent.ACTION_GET_CONTENT)
    chooseFile.type = MIME_TYPE_IMAGE
    chooseFile = Intent.createChooser(chooseFile, resources.getString(R.string.choose_file))
    startActivityForResult(chooseFile, PICK_FILE_RESULT_CODE)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    when (requestCode) {
        PICK_FILE_RESULT_CODE -> if (resultCode == -1) {
            data!!.data?.let { returnUri ->
                loadPicture(returnUri)
                binding.constraintLayout.visibility = View.VISIBLE
                viewModel.imagePath.value = returnUri
            }
        }
    }
}

private fun loadPicture(uri: Uri) {
    Picasso.get()
        .load(uri)
        .error(R.drawable.ic_broken_image_gray_24dp)
        .into(binding.placeImage)
}

In my ViewHolder I’m using the following code to display the image (image path saved in Room DB as String):

fun bind(location: Location) {
    textViewLocationName.text = location.name
    Picasso.get()
        .load(Uri.parse(location.imagePath!!))
        .error(R.drawable.ic_broken_image_gray_24dp)
        .into(imageViewLocation)
}

When debugging the URI looks like this: content://com.android.providers.media.documents/document/image%3A4569

Is there some kind of formatting issue with the URI? Or is there another problem?

Thanks!

Comments

Comment posted by but not in future processes

For

READ  [ANSWERED] android - how to use a dagger fragment viewmodel in injected dagger fragment?
Powered by Inline Related Posts

Comment posted by susosaurus

thank you so much for this! works like a charm 🙂

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress