如何将Glide显示的图像下载到设备存储中。科特林

发布于 2025-02-08 11:54:16 字数 1649 浏览 3 评论 0原文

我有一个Recyclerview,它使用Glide显示Firebase实时数据库中的图像。我在此RecyClerview中有一个下载按钮,希望用户在单击下载按钮后能够下载这些图像。我听说您可以使用Glide下载图像来进行内部存储,但是我对此不熟悉,因此我希望您的帮助。

适配器类

class AbstractAdapter(private val mContext: Context, private val abstractList: ArrayList<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.imageView.setOnClickListener {
        val intent = Intent(mContext, AbstractPreview::class.java)
        intent.putExtra("abstract", abstractList[position].abstract.toString())
        Toast.makeText(mContext, "Fullscreen view", Toast.LENGTH_SHORT).show()
        mContext.startActivity(intent)

    }

    holder.downloadBtn.setOnClickListener {
        
    }

    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 downloadBtn: Button = itemView.findViewById(R.id.abstractDownloadBtn)

}

companion object {
    private const val Tag = "RecyclerView"
}

这是下载按钮的onclicklister。

holder.downloadBtn.setOnClickListener {

        }

如您所见,我知道如何使用滑行显示图像。我需要知道的是如何将这些图像下载到设备内部存储中。

I have a recyclerview that uses Glide to show images from Firebase Realtime database. I have a download button in this recyclerview and I want users to be able to download these images once the click the download button. I heard you can use Glide to download images to device internal storage, but I'm unfamiliar with this, so I would like your assistance.

Adapter class

class AbstractAdapter(private val mContext: Context, private val abstractList: ArrayList<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.imageView.setOnClickListener {
        val intent = Intent(mContext, AbstractPreview::class.java)
        intent.putExtra("abstract", abstractList[position].abstract.toString())
        Toast.makeText(mContext, "Fullscreen view", Toast.LENGTH_SHORT).show()
        mContext.startActivity(intent)

    }

    holder.downloadBtn.setOnClickListener {
        
    }

    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 downloadBtn: Button = itemView.findViewById(R.id.abstractDownloadBtn)

}

companion object {
    private const val Tag = "RecyclerView"
}

This is the onClickLister for the download button.

holder.downloadBtn.setOnClickListener {

        }

As you can see, I know how to show images using Glide. All I need to know is how to download those images into the device internal storage.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伊面 2025-02-15 11:54:16
  1. 添加OKHTTP依赖关系

首先

implementation("com.squareup.okhttp3:okhttp:4.10.0")

,您需要将OKHTTP依赖性依赖于您的应用程序gradle: 注意:如果您正在进行改造,则不需要添加Okhttp依赖关系依赖性

  1. 创建下载功能

OK现在我们将添加下载逻辑在您的视图模型中,将OKHTTP实例声明和下载函数添加到您的视图模型:

注意:您可以将下载逻辑移至存储库或任何您想要的任何地方,这取决于您。

class YourViewModel : ViewModel() {

    // add okhttp instance to your view model or you inject it with hilt if your using dependency injection
    private val okHttpClient = OkHttpClient.Builder().build()

    // add this function to your view model
    fun downloadImage(imageUrl: String) {
        val request = Request.Builder()
            .url(imageUrl)
            .build()

        okHttpClient.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                // Download Failed, you can show error to the user
            }

            override fun onResponse(call: Call, response: Response) {
                if (!response.isSuccessful) {
                    // Download Failed, you can show error to the user
                    return
                }

                response.body?.let { responseBody ->
                    try {
                        // Convert response body to byte array
                        val imageByteArray = responseBody.byteStream().readBytes()

                        // Split image url so we can get the image name
                        val words = imageUrl.split("/").toTypedArray()

                        // Get the image name
                        val imageName = words.last()

                        // Init pathName (Downloads Directory)
                        val pathName = "${Environment.getExternalStorageDirectory()}/${Environment.DIRECTORY_DOWNLOADS}"

                        // Create New file for the image
                        val file = File(pathName, imageName)

                        // Set byteArray To Image File
                        file.writeBytes(imageByteArray)
                    } catch(e: IOException) {
                        // Saving Image Failed, you can show error to the user
                        e.printStackTrace()
                    }
                }
            }
        })
    }

}
  1. 下载函数从片段到适配器传递给

现在您需要将

val adapter = AbstractAdapter(
    context = requireContext(),
    abstractList = abstractList, // your list here
    downloadImage = { imageUrl ->
        viewModel.downloadImage(imageUrl)
    }
)

您的适配器,您需要将下载函数传递给lambda函数中的适配器,片段中的适配器创建应该看起来像:您的适配器构造函数看起来像这样:

class AbstractAdapter(
    private val mContext: Context, 
    private val abstractList: ArrayList<Abstract>,
    private val downloadImage: (imageUrl: String) -> Unit
): RecyclerView.Adapter<AbstractAdapter.ViewHolder>()
  1. call downloctimage lambda lambda inslow downloctbtn inslow downloctbtn单击侦听器

现在,我们将在单击“侦听器”中添加下载图像

holder.downloadBtn.setOnClickListener {
    val imageUrl = abstractList[position].abstract
    downloadImage(imageUrl)
}
  1. 调用添加写入外部存储权限到androidmanifest.xml

将此许可添加到您的androidmanifest.xml文件中,以便能够将文件添加到电话存储

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="28" />

注意:此权限此权限仅限SQK版本&lt; = 28这就是为什么我们添加了android:maxSdkversion =“ 28”

我希望一切都清楚。
此代码应该给您想要的结果,请尝试一下,并告诉我是否有任何错误。

  1. Add Okhttp dependency

First of all you need to have okhttp dependency on your app gradle:

implementation("com.squareup.okhttp3:okhttp:4.10.0")

Note: if you are having retrofit no need to add okhttp dependency

  1. Create download function

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.

class YourViewModel : ViewModel() {

    // add okhttp instance to your view model or you inject it with hilt if your using dependency injection
    private val okHttpClient = OkHttpClient.Builder().build()

    // add this function to your view model
    fun downloadImage(imageUrl: String) {
        val request = Request.Builder()
            .url(imageUrl)
            .build()

        okHttpClient.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                // Download Failed, you can show error to the user
            }

            override fun onResponse(call: Call, response: Response) {
                if (!response.isSuccessful) {
                    // Download Failed, you can show error to the user
                    return
                }

                response.body?.let { responseBody ->
                    try {
                        // Convert response body to byte array
                        val imageByteArray = responseBody.byteStream().readBytes()

                        // Split image url so we can get the image name
                        val words = imageUrl.split("/").toTypedArray()

                        // Get the image name
                        val imageName = words.last()

                        // Init pathName (Downloads Directory)
                        val pathName = "${Environment.getExternalStorageDirectory()}/${Environment.DIRECTORY_DOWNLOADS}"

                        // Create New file for the image
                        val file = File(pathName, imageName)

                        // Set byteArray To Image File
                        file.writeBytes(imageByteArray)
                    } catch(e: IOException) {
                        // Saving Image Failed, you can show error to the user
                        e.printStackTrace()
                    }
                }
            }
        })
    }

}
  1. Pass download function from your fragment to your adapter

Now you need to pass your download function to your adapter inside a lambda function, adapter creation in your fragment should look like this:

val adapter = AbstractAdapter(
    context = requireContext(),
    abstractList = abstractList, // your list here
    downloadImage = { imageUrl ->
        viewModel.downloadImage(imageUrl)
    }
)

Your adapter constructor will look like this:

class AbstractAdapter(
    private val mContext: Context, 
    private val abstractList: ArrayList<Abstract>,
    private val downloadImage: (imageUrl: String) -> Unit
): RecyclerView.Adapter<AbstractAdapter.ViewHolder>()
  1. Call downloadImage lambda inside downloadBtn click listener

now we will add downloadImage call inside the click listener

holder.downloadBtn.setOnClickListener {
    val imageUrl = abstractList[position].abstract
    downloadImage(imageUrl)
}
  1. Add write external storage permission to AndroidManifest.xml

add this permission to your AndroidManifest.xml file to be able to add files to phone storage

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="28" />

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文