android - 使用 FirebaseStored 下载 SVG 数据图像
问题描述
我想使用 data 方法下载 FirebaseStore 中的 svg 图像,但位图转换返回 null。我需要返回 svg 图像并放在屏幕上
private lateinit var auth: FirebaseAuth
private lateinit var storage: FirebaseStorage
private lateinit var storageRef: StorageReference
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
auth = Firebase.auth
storage = Firebase.storage
storageRef = storage.reference
val fileRef = storageRef.child("CFM/cfm2.back.svg")
downloadImgData(fileRef)
}
private fun downloadImgData(fileRef: StorageReference?){
if (fileRef != null) {
val ONE_MEGABYTE = (1024 * 1024).toLong()
fileRef.getBytes(ONE_MEGABYTE)
.addOnSuccessListener { bytes ->
val bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
imageView.setImageBitmap(bmp)
textview.text = fileRef.name
}
.addOnFailureListener { exception ->
Toast.makeText(this, exception.message, Toast.LENGTH_LONG).show()
}
}
解决方案
您可以使用Pixplicity 的 Sharp将 SVG 图像加载到ImageView
. 它提供了将 svg 图像从加载InputStream
到ImageView
.
ByteArray
从 Firebase Storage 获得的可以InputStream
通过调用bytes.inputStream()
.
添加库依赖
dependencies {
implementation 'com.pixplicity.sharp:library:[VERSION_HERE]@aar'
}
这会将 SVG 图像加载到 imageView
val stream = bytes.inputStream()
Sharp.loadInputStream(stream).into(imageView)
stream.close()
最后函数看起来像,
private fun downloadImgData(fileRef: StorageReference?) {
if (fileRef != null) {
val ONE_MEGABYTE = (1024 * 1024).toLong()
fileRef.getBytes(ONE_MEGABYTE)
.addOnSuccessListener { bytes ->
val stream = bytes.inputStream()
Sharp.loadInputStream(stream).into(imageView)
stream.close()
textview.text = fileRef.name
}
.addOnFailureListener { exception ->
Toast.makeText(this, exception.message, Toast.LENGTH_LONG).show()
}
}
}
推荐阅读
- jquery - 使用 jQuery 在 DOM 中添加事件后单击不起作用
- mysql - 使用通配符在 MySQL 数据库中查找重复数据
- python - vtkTextActor 不起作用
- algorithm - 在给定索引列表的情况下查找最接近的索引
- python - 如何将输入放入函数中
- oracle - 在 oracle ucm web center 中签入文档时发生异常
- karate - 从空手道中的 json 设置字段值
- python - 正则表达式 - 匹配后捕获单词
- python - Django - 在子路径中运行时出现静态文件的问题
- amazon-web-services - Cognito 保存用户池中的更改不起作用