0

This is possibly a duplicate question but none of the codes helped me and some answers are old and now deprecated. I'm making an android application and in that I want to retrieve and store the user's profile picture in my app. The uri of the image is provided by FirebaseUser class as uri but I want to know how to retrieve bitmap from the uri and save it in apps 'filesDir()' folder and get a drawable image from it whenever I want it. I have obviously tried some codes but none of it is working. Two ways I have tried :-

fun getBitmap(context: Context, selectedImage: Uri): Bitmap? {
  val imageStream = context.contentResolver.openInputStream(selectedImage)
  val image = BitmapFactory.decodeStream(imageStream, null, BitmapFactory.Options())
  imageStream?.close()
  return image
}

Another method I found was using ImageDecoder and MediaStore's getBitmap() (which is deprecated)

 val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    ImageDecoder.decodeBitmap(ImageDecoder.createSource(contentResolver, Uri.parse("https://lh3.googleusercontent.com/a-/link"))) //here parsing a google account profile pic link
             } else {
                    MediaStore.Images.Media.getBitmap(contentResolver, Uri.parse("https://lh3.googleusercontent.com/a-/link"))
             }

The problem is whenever I try any of it the app just keeps crashing with exception java.io.FileNotFoundException: No content provider.
Am I missing something? Do we need any manifest configuration in app to access uri from internet?

1 Answers1

0

I recommend you to use Glide, here is the documentation: https://github.com/bumptech/glide

Glide is a library where by just calling their method and passing the URL you need you are going to be good.


I will explain to you how to prepare it,

Download it using Gradle,

 repositories {
   mavenCentral()
   google()
 }

 dependencies {
   implementation 'com.github.bumptech.glide:glide:4.10.0'
   annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'
 }

Then Use it :)

imageView = findViewById(R.id.imageView)
Glide.with(this).load("http://yoururl.png").into(imageView)

This is to Save the Image into Firebase: How to save bitmap to Firebase

I recommend your app works with Firebase database and you save your image URL'S into your user profile in the database

best, and do magic :)

ElDeivis
  • 50
  • 6