【发布时间】:2022-06-11 08:20:03
【问题描述】:
我有一个 recyclerview,它显示来自 Firebase 实时数据库的多个图像。 recyclerview 里面也有一个按钮。我希望此按钮允许用户在点击后一次一张下载这些图片。
一旦用户单击“下载”,我希望将图像保存到他们的设备存储中。我为此尝试了多种解决方案,但它们对 Firestore 数据库都没有帮助,或者只允许下载一个图像。
代码
class AbstractAdapter(private val mContext: Context, private val abstractList: List<Abstract>) :
RecyclerView.Adapter<AbstractAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.abstract_image_view, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.download_btn.setOnClickListener {
downloadFile()
}
Glide.with(mContext)
.load(abstractList[position].abstract)
.into(holder.imageView)
}
override fun getItemCount(): Int {
return abstractList.size
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val imageView: ImageView = itemView.findViewById(R.id.abstractImageView)
val download_btn: Button = itemView.findViewById(R.id.abstractDownloadBtn)
}
private fun downloadFile() {
val storage = FirebaseStorage.getInstance()
val storageRef = storage.getReferenceFromUrl("https://notes-72413.firebaseio.com/")
val islandRef = storageRef.child("Abstract")
val rootPath = File(Environment.getExternalStorageDirectory(), "abstract")
if (!rootPath.exists()) {
rootPath.mkdirs()
}
val localFile = File(rootPath, "imageName.jpeg")
islandRef.getFile(localFile)
.addOnSuccessListener(OnSuccessListener<FileDownloadTask.TaskSnapshot?> {
Log.e("firebase ", ";local tem file created created $localFile")
// updateDb(timestamp,localFile.toString(),position);
}).addOnFailureListener(OnFailureListener { exception ->
Log.e(
"firebase ",
";local tem file not created created $exception"
)
})
}
companion object {
private const val Tag = "RecyclerView"
}
我试过这段代码,但是一旦我点击“下载”按钮,它立即崩溃,Logcat 说Firebase Storage URLs must point to an object in your Storage Bucket. Please obtain a URL using the Firebase Console or getDownloadUrl()
我的 Firebase 实时数据库
共有 64 个文件
总结
我有一个显示来自 Firebase 实时数据库的图像的回收视图。一旦用户点击“下载”按钮,它一次只会将一张图片下载到他们的设备存储中。
更新
private fun downloadFile() {
val storage = FirebaseStorage.getInstance()
val storageRef = storage.getReferenceFromUrl("abstract")
val rootPath = File(Environment.getExternalStorageDirectory(), "abstract")
if (!rootPath.exists()) {
rootPath.mkdirs()
}
val localFile = File(rootPath, "imageName.jpeg")
storageRef.child("Abstract").downloadUrl.addOnSuccessListener { Log.e("firebase ", ";local tem file created created $localFile")
}.addOnFailureListener(OnFailureListener { exception ->
Log.e("firebase ", ";local tem file not created created $exception")
})
}
这些是我对 downloadFile 函数所做的更改,但我仍然收到错误:
The storage Uri could not be parsed
【问题讨论】:
-
您是否尝试过使用错误消息中存在的方法?
-
我刚才试过了。它给了我一个错误。未知参考
标签: firebase kotlin firebase-realtime-database android-recyclerview android-download-manager