如何从 android (kotlin) 中 Firebase 提供的照片 uri 检索和存储用户个人资料图片?
How to retrieve and store user profile picture from photo uri provided by Firebase in android (kotlin)?
这可能是一个重复的问题,但 none 的代码对我有帮助,有些答案是旧的,现在已弃用。我正在制作一个 android 应用程序,我想在我的应用程序中检索和存储用户的个人资料图片。图像的 uri 由 FirebaseUser class 作为 uri 提供,但我想知道如何从 uri 检索位图并将其保存在 apps 'filesDir()' 文件夹中,并在需要时从中获取可绘制图像.我显然已经尝试了一些代码,但其中 none 有效。
我尝试过的两种方法:-
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 中:How to save bitmap to Firebase
我建议您的应用与 Firebase 数据库配合使用,并将图像 URL 保存到数据库中的用户个人资料中
最好的,
并施展魔法 :)
这可能是一个重复的问题,但 none 的代码对我有帮助,有些答案是旧的,现在已弃用。我正在制作一个 android 应用程序,我想在我的应用程序中检索和存储用户的个人资料图片。图像的 uri 由 FirebaseUser class 作为 uri 提供,但我想知道如何从 uri 检索位图并将其保存在 apps 'filesDir()' 文件夹中,并在需要时从中获取可绘制图像.我显然已经尝试了一些代码,但其中 none 有效。 我尝试过的两种方法:-
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 中:How to save bitmap to Firebase
我建议您的应用与 Firebase 数据库配合使用,并将图像 URL 保存到数据库中的用户个人资料中
最好的, 并施展魔法 :)