How to receive path from internal storage in android and transform it into view?

58 Views Asked by At

I'm new to android development and I've been building an app for studies purposes that is supposed to display one of the images selected from the gallery previously in a recyclerview.

private fun configDocImageDialog(pathGallery: Int, pathCamera: Int) {
        MaterialAlertDialogBuilder(requireContext())
            .setTitle(getString(R.string.camera_or_gallery))
            .setMessage(getString(R.string.image_path))
            .setPositiveButton(getString(R.string.gallery)) { _, _ ->
                val intent = Intent()
                intent.type = "image/*"
                intent.action = Intent.ACTION_GET_CONTENT
                startActivityForResult(Intent.createChooser(intent, "Select: "), pathGallery)
            }.setNegativeButton(getString(R.string.camera)) { _, _ ->
                val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
                startActivityForResult(Intent.createChooser(intent, "Take: "), pathCamera)
            }.setNeutralButton(getString(R.string.cancel)) { dialog, _ ->
                dialog.dismiss()
            }.show()
    }

Not I'm not worried about the camera result. Then receiving the result as:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(resultCode == RESULT_OK) {
            val selectedImage = data!!.data
            val pathImage = selectedImage.toString()
            try {
                when (requestCode) {
                    GALLERY_1 -> {
                        binding.imageDoc1.setImageURI(selectedImage)
                    }
                    CAMERA_1 -> {
                        binding.imageDoc1.setImageURI(selectedImage)
                    }
                    GALLERY_2 -> {
                        binding.imageDoc2.setImageURI(selectedImage)
                    }
                    CAMERA_2 -> {
                        binding.imageDoc2.setImageURI(selectedImage)
                    }
                    GALLERY_3 -> {
                        binding.imageDoc3.setImageURI(selectedImage)
                    }
                    CAMERA_3 -> {
                        binding.imageDoc3.setImageURI(selectedImage)
                    }
                }
                imageList.add(pathImage)
            } catch (e: Exception){
                e.printStackTrace()
            }
        }
    }

I received as a result from the intent a list with this kind of content:

content://com.android.providers.media.documents/document/image%3A20

And those are saved into the database, the list of paths. / Is it possible to use this path to my exhibit the image in my adapter? I've been trying different treatments but it always blank. I've trying to use Picasso as:

override fun onBindViewHolder(holder: DocViewHolder, position: Int) {
        val doc = docs[position]
        holder.binding.apply {
            Picasso.get()
                .load(doc.docImageList[0])
                .into(imageDoc)
            textDocName.text = doc.title
            textValidity.text = doc.validity
        }
        holder.itemView.setOnClickListener {
            onItemClickListener?.let {
                it(doc)
            }
        }
    }

I want to display an image from internal storage in a recyclerview but the image is always blank. Any ideas how to do it properly? Thanks

0

There are 0 best solutions below