我正在尝试使用 Glide 从 URL 下载图像并获取文件的路径并将其转发以WallpaperManager.getCropAndSetWallpaperIntent设置为墙纸。
我发现这可以使用asFileGlide 的方法来完成
科特林:
val data = Glide
.with(context)
.asFile()
.load(url)
.submit()
但是当我打电话时data.get()我得到了错误
java.lang.IllegalArgumentException: You must call this method on a background thread
所以遵循这个答案并实施MyAsyncTask
interface AsyncResponse {
fun processFinish(output: File?)
}
class MyAsyncTask(delegate: AsyncResponse) : AsyncTask<FutureTarget<File>, Void, File?>() {
override fun doInBackground(vararg p0: FutureTarget<File>?): File? {
return p0[0]?.get()
}
private var delegate: AsyncResponse? = null
init {
this.delegate = delegate
}
override fun onPostExecute(result: File?) {
delegate!!.processFinish(result)
}
}
而我现在正在这样做
fun getFile(context: Context, url: String) : File {
val data = Glide
.with(context)
.asFile()
.load(url)
.submit()
val asyncTask = MyAsyncTask(object : AsyncResponse {
override fun processFinish(output: File?) {
println(output?.path)
}
}).execute(data)
return asyncTask.get()
}
但我似乎无法得到File
狐的传说
斯蒂芬大帝
相关分类