I'm trying to download an image from a URL using Glide and get the path of the file and forward it to WallpaperManager.getCropAndSetWallpaperIntent
to be set as a wallpaper.
I found that this can be done using asFile
method of Glide
Kotlin:
val data = Glide
.with(context)
.asFile()
.load(url)
.submit()
But when I call data.get()
I get the error
java.lang.IllegalArgumentException: You must call this method on a background thread
So followed this answer and implemented 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)
}
}
And I'm doing this now
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()
}
But I can't seem to get the File
Edit: It was working but now there's a new error
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.service.wallpaper.CROP_AND_SET_WALLPAPER dat=content://com.rithvij.scrolltest.provider/cache/image_manager_disk_cache/efebce47b249d7d92fd17340ecf91eb6b7ff86f91d71aabf50468f9e74d0e324.0 flg=0x1 pkg=is.shortcut }
Full stack trace
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.rithvij.scrolltest, PID: 2760
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.service.wallpaper.CROP_AND_SET_WALLPAPER dat=content://com.rithvij.scrolltest.provider/cache/image_manager_disk_cache/efebce47b249d7d92fd17340ecf91eb6b7ff86f91d71aabf50468f9e74d0e324.0 flg=0x1 pkg=is.shortcut }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1816)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1525)
at android.app.Activity.startActivityForResult(Activity.java:4396)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
at android.app.Activity.startActivityForResult(Activity.java:4355)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
at android.app.Activity.startActivity(Activity.java:4679)
at android.app.Activity.startActivity(Activity.java:4647)
at com.rithvij.scrolltest.MainActivity$onCreate$1.onClick(MainActivity.kt:71)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22298)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
- Now my question is is this the preferred way to set wallpaper from a url?
- How to deal with the other error?