-1

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)
  1. Now my question is is this the preferred way to set wallpaper from a url?
  2. How to deal with the other error?
Phani Rithvij
  • 4,030
  • 3
  • 25
  • 60

2 Answers2

0

Regarding your first question about getting image from url, instead of using asFile, it is recommended that you use the method downloadOnly(). Then, rather than using an AsyncTask, you can leverage a RequestListener to get an async callback when the resource is loaded.

As for your second question, you are broadcasting an Implicit Intent that is not registered by the OS or any apps on your device. Rather than broadcasting an intent, you can try leveraging the WallpaperManager System Service.

Elli White
  • 1,440
  • 1
  • 12
  • 21
0

Answering my own question

  1. It is better to use downloadOnly() as suggested by Elli White here. But I wasted enough time on researching this question and I got a working solution so I decided not to start from scratch.

  2. The error I got was because of the image file name that's being returned by Glide.

I fixed it by copying the file somewhere and using it as the source.

        val file = asyncTask.get()
//        copy file
        val tempFile = File.createTempFile("image", ".png")
        copyFile(file!!.inputStream(), FileOutputStream(tempFile))

And for my use case i.e. to set the image as wallpaper I need not worry about the file extension as long as I specify that it's an image .png in this case.

Phani Rithvij
  • 4,030
  • 3
  • 25
  • 60