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?