android - 如何从 Android(kotlin)中 Firebase 提供的照片 uri 中检索和存储用户个人资料图片?
问题描述
这可能是一个重复的问题,但没有任何代码对我有帮助,而且有些答案是旧的,现在已弃用。我正在制作一个 android 应用程序,并且我想在我的应用程序中检索和存储用户的个人资料图片。图像的 uri 由 FirebaseUser 类作为 uri 提供,但我想知道如何从 uri 中检索位图并将其保存在应用程序的“filesDir()”文件夹中,并在需要时从中获取可绘制的图像。我显然尝试了一些代码,但没有一个有效。我尝试过的两种方法:-
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
}
我发现的另一种方法是使用 ImageDecoder 和 MediaStore getBitmap()
(已弃用)
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"))
}
问题是每当我尝试其中任何一个时,应用程序都会不断崩溃并出现异常java.io.FileNotFoundException: No content provider
。
我错过了什么吗?我们是否需要在应用程序中进行任何清单配置才能从互联网访问 uri?
解决方案
我推荐你使用 Glide,这里是文档:https ://github.com/bumptech/glide
Glide 是一个库,只需调用他们的方法并传递您需要的 URL,您就会变得很好。
我会告诉你如何准备它,
使用 Gradle 下载它,
repositories {
mavenCentral()
google()
}
dependencies {
implementation 'com.github.bumptech.glide:glide:4.10.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'
}
然后使用它:)
imageView = findViewById(R.id.imageView)
Glide.with(this).load("http://yoururl.png").into(imageView)
这是将图像保存到 Firebase:如何将位图保存到 Firebase
我建议您的应用与 Firebase 数据库一起使用,并将您的图片 URL 保存到数据库中的用户配置文件中
最好的,做魔术:)
推荐阅读
- arrays - 循环遍历 mongodb 聚合——更好的方法是什么?
- javascript - 基于所选选项的 jQuery 搜索脚本
- javascript - Playwright - TypeError:hoverElement.hover 不是函数
- arduino - SIM900L 如何读取所有电话簿条目并存储在数组中?
- python - Python:在子类实例(tkinter)上使用父方法时获取 AttributeError
- python - 使用 describe() 方法排除列
- javascript - shopify 的“OptionSelectors”实际需要哪些参数?
- linq - 如何复制列表
- > 在 LINQ 中上课?
- javascript - 从 Fetch() 获取 PromiseResult
- docker - 容器未运行