将 GIF 下载为文件并获取内容 URI
Download GIF as a file and take it content URI
我需要将GIF输入连接https://developer.android.com/guide/topics/text/image-keyboard.html
但是我有一个问题,因为我需要获取这个 GIF 的 uri。我像下载文件一样下载它,只能获取它的路径。我怎样才能得到 URI?或者我需要以其他方式执行此操作?
Glide.with(this)
.asFile()
.load(contentURL)
.downloadOnly(object: SimpleTarget<File>(){
override fun onResourceReady(resource: File, transition: Transition<in File>?) {}
更新(已测试)
选项 1:使用 Glide
private suspend fun downloadImage(context: Context, imageUrl: String, fileName: String, folderName: String) {
withContext(Dispatchers.IO) {
val gifBuffer = Glide.with(context).asGif().load(imageUrl).submit().get().buffer
val imageUri = getImageUri(getApplication(), fileName, folderName)
context.contentResolver.openOutputStream(imageUri).use {
val bytes = ByteArray(gifBuffer.capacity())
(gifBuffer.clear() as ByteBuffer).get(bytes) //Both ".clear()" and "as ByteBuffer" are mandatory!!
it?.write(bytes)
}
}
}
选项 2:使用 URL(url).openStream()
private suspend fun downloadImage(context: Context, imageUrl: String, fileName: String, folderName: String) {
withContext(Dispatchers.IO) {
val imageUri = getImageUri(getApplication(), fileName, folderName)
URL(imageUrl).openStream().use { inputStream ->
context.contentResolver.openOutputStream(imageUri).use { outputStream ->
inputStream.copyTo(outputStream!!)
}
}
}
}
我觉得你应该用DownloadManager
来实现,其中有setDestinationUri()
.
记得先生成镜像文件uri
:
private fun getNewImageUri(fileName: String, folderName: String): Uri { //create new file
val values = ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, fileName) //file name
put(MediaStore.MediaColumns.MIME_TYPE, "image/gif") //file type
put(MediaStore.MediaColumns.RELATIVE_PATH, "${Environment.DIRECTORY_DOWNLOAD}/$folderName") //file location
}
return requireContext().contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)!!
}
我需要将GIF输入连接https://developer.android.com/guide/topics/text/image-keyboard.html
但是我有一个问题,因为我需要获取这个 GIF 的 uri。我像下载文件一样下载它,只能获取它的路径。我怎样才能得到 URI?或者我需要以其他方式执行此操作?
Glide.with(this)
.asFile()
.load(contentURL)
.downloadOnly(object: SimpleTarget<File>(){
override fun onResourceReady(resource: File, transition: Transition<in File>?) {}
更新(已测试)
选项 1:使用 Glide
private suspend fun downloadImage(context: Context, imageUrl: String, fileName: String, folderName: String) {
withContext(Dispatchers.IO) {
val gifBuffer = Glide.with(context).asGif().load(imageUrl).submit().get().buffer
val imageUri = getImageUri(getApplication(), fileName, folderName)
context.contentResolver.openOutputStream(imageUri).use {
val bytes = ByteArray(gifBuffer.capacity())
(gifBuffer.clear() as ByteBuffer).get(bytes) //Both ".clear()" and "as ByteBuffer" are mandatory!!
it?.write(bytes)
}
}
}
选项 2:使用 URL(url).openStream()
private suspend fun downloadImage(context: Context, imageUrl: String, fileName: String, folderName: String) {
withContext(Dispatchers.IO) {
val imageUri = getImageUri(getApplication(), fileName, folderName)
URL(imageUrl).openStream().use { inputStream ->
context.contentResolver.openOutputStream(imageUri).use { outputStream ->
inputStream.copyTo(outputStream!!)
}
}
}
}
我觉得你应该用DownloadManager
来实现,其中有setDestinationUri()
.
记得先生成镜像文件uri
:
private fun getNewImageUri(fileName: String, folderName: String): Uri { //create new file
val values = ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, fileName) //file name
put(MediaStore.MediaColumns.MIME_TYPE, "image/gif") //file type
put(MediaStore.MediaColumns.RELATIVE_PATH, "${Environment.DIRECTORY_DOWNLOAD}/$folderName") //file location
}
return requireContext().contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)!!
}