如何使用Glide下载图像到设备内部存储
我有一个Recyclerview,它使用GLIDE显示Firebase实时数据库中的图像。在RecyClerview中,我还有一个下载按钮。当用户单击按钮时,希望将Firebase的图像下载到设备内部存储中。
适配器类
class NatureAdapter(private val mContext: Context, private val natureList: ArrayList<Nature>) : RecyclerView.Adapter<NatureAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.nature_image_view, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
Glide.with(mContext)
.load(natureList[position].space)
.into(holder.imageView)
}
override fun getItemCount(): Int {
return natureList.size
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var imageView: ImageView = itemView.findViewById(R.id.NatureView)
}
companion object {
private const val Tag = "RecyclerView"
}
}
数据列表类别
class Nature (var space: String? = null) {
}
更新
第一个错误
e: C:\Users\Khumo Kwezi Mashapa\AndroidStudioProjects\MyNotepad\app\src\main\java\com\khumomashapa\notes\fragments\NatureWallpapers.kt: (65, 17): Unresolved reference: viewModel
第二次错误
e: C:\Users\Khumo Kwezi Mashapa\AndroidStudioProjects\MyNotepad\app\src\main\java\com\khumomashapa\notes\fragments\NatureWallpapers.kt: (120, 93): No value passed for parameter 'downloadImage'
I have a recyclerview that uses Glide to display images from Firebase Realtime database. In the recyclerview I also have a download button. When a user clicks the button, want the image from Firebase to be downloaded into the device internal storage.
Adapter class
class NatureAdapter(private val mContext: Context, private val natureList: ArrayList<Nature>) : RecyclerView.Adapter<NatureAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.nature_image_view, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
Glide.with(mContext)
.load(natureList[position].space)
.into(holder.imageView)
}
override fun getItemCount(): Int {
return natureList.size
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var imageView: ImageView = itemView.findViewById(R.id.NatureView)
}
companion object {
private const val Tag = "RecyclerView"
}
}
Data list class
class Nature (var space: String? = null) {
}
Update
First error
e: C:\Users\Khumo Kwezi Mashapa\AndroidStudioProjects\MyNotepad\app\src\main\java\com\khumomashapa\notes\fragments\NatureWallpapers.kt: (65, 17): Unresolved reference: viewModel
Second error
e: C:\Users\Khumo Kwezi Mashapa\AndroidStudioProjects\MyNotepad\app\src\main\java\com\khumomashapa\notes\fragments\NatureWallpapers.kt: (120, 93): No value passed for parameter 'downloadImage'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先
,您需要将OKHTTP依赖性依赖于您的应用程序gradle: 注意:如果您正在进行改造,则不需要添加Okhttp依赖关系依赖性
OK现在我们将添加下载逻辑在您的视图模型中,将OKHTTP实例声明和下载函数添加到您的视图模型:
注意:您可以将下载逻辑移至存储库或任何您想要的任何地方,这取决于您。
现在您需要将
您的适配器,您需要将下载函数传递给lambda函数中的适配器,片段中的适配器创建应该看起来像:您的适配器构造函数看起来像这样:
现在,我们将在单击“侦听器”中添加下载图像
将此许可添加到您的androidmanifest.xml文件中,以便能够将文件添加到电话存储
注意:此权限此权限仅限SQK版本&lt; = 28这就是为什么我们添加了
android:maxSdkversion =“ 28”
我希望一切都清楚。
此代码应该给您想要的结果,请尝试一下,并告诉我是否有任何错误。
First of all you need to have okhttp dependency on your app gradle:
Note: if you are having retrofit no need to add okhttp dependency
Ok now we are going to add the download logic in your view model, add okhttp instance declaration and download function to your view model:
Note: you can move download logic to a repository or anywhere you want, it's up to you.
Now you need to pass your download function to your adapter inside a lambda function, adapter creation in your fragment should look like this:
Your adapter constructor will look like this:
now we will add downloadImage call inside the click listener
add this permission to your AndroidManifest.xml file to be able to add files to phone storage
Note: This permission is only required for sqk version <= 28 that's why we added
android:maxSdkVersion="28"
I hope that everything is clear.
This code should give you the result that you want, try it and tell me if there is anything wrong.
您也可以使用下载经理Bruv。只需创建一个OnItemClick界面,只需在主片段或类中实现此功能即可。
You can also use Download Manager bruv. Just create an OnItemClick interface and simply implement this function in your main fragment or class.