Home > database >  Problem Glide showing picture from local Uri
Problem Glide showing picture from local Uri

Time:12-03

I'm trying to show a picture stored in my device using Glide but it doesn't work. My result Uri is:

file:///data/user/0/com.example.tfg_red_social_v0/files/croppedImage.jpg

The file exists I've checked with the explorer

private val cropImage = registerForActivityResult(uCropContract){uri->
     Glide.with(this).load(uri).circleCrop().into(binding.ivPerfil)
}

Please any help is welcome I'm adding extra code

My manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <!-- PERMISOS PARA ACCEDER A INTERNET, CAMARA Y SD -->
    <uses-permission android:name="android.permission.INTERNET" />
   <!-- <uses-permission android:name="android.permission.CAMERA" /> -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" tools:ignore="ScopedStorage"/>
    <uses-feature android:name="android.hardware.camera" android:required="true" />

this is my provider

<provider
            android:authorities="com.example.tfg_red_social_v0.fileprovider"
            android:name="androidx.core.content.FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

and my file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-files-path
        name="my_debug_images"
        path="Pictures"/>
</paths>

And here how I use UCROP

    private val uCropContract = object: ActivityResultContract<List<Uri>, Uri>(){
        override fun createIntent(context: Context, input: List<Uri>): Intent {
            val inputUri =  input[0]
            val outPut =  input[1]
     
            val uCrop = UCrop.of(inputUri,outPut)
                .withAspectRatio(16f,9f)
            return uCrop.getIntent(context)

        }

        override fun parseResult(resultCode: Int, intent: Intent?): Uri {
            return UCrop.getOutput(intent!!)!!
        }
    }

    private val cropImage = registerForActivityResult(uCropContract){uri->
        imagenUri=uri
        Glide.with(this).load(uri).into(binding.ivImagen)
      }

    private val getContent = registerForActivityResult(ActivityResultContracts.GetContent()){uri->
        val inputUri = uri
        val outputUri = File(requireContext().filesDir, "croppedImage.jpg").toUri()
        val listUri = listOf<Uri>(inputUri!!,outputUri)
        cropImage.launch(listUri)
    }
}

And I start crop with

  binding.ivImagen.setOnClickListener{
        getContent.launch("image/*")
    }

CodePudding user response:

Your Glide usage seems correct, do you have the second dependency added, please check your build.gradle(:app):

    implementation 'com.github.bumptech.glide:glide:4.12.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'

Edit: In your manifest check if you have this permissions:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        tools:ignore="ScopedStorage" />

And this in between your application tags in manifest:

        <provider
            android:name="androidx.core.content.FileProvider"
            
            android:authorities="com.example.tfg_red_social_v0.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

And add file_paths.xml contains below code in your res/xml folder:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="my_images"
        path="Android/data/com.example.tfg_red_social_v0/files/Pictures" />
</paths>

If it's not refreshing image then clear its cache by adding like:

Glide.get(this).clearMemory()
Glide.with(this)
     .load(uri)
     .circleCrop()
     .skipMemoryCache(true)
     .diskCacheStrategy(DiskCacheStrategy.NONE)
     .into(binding.ivPerfil)
  • Related