I'm making an image-downloading app that sets the image as the device wallpaper. For this I used the class:


This class has a function which accepts a URL and an ImageView. It downloads and assigns the image found at the URL to the ImageView using an AsyncTask class defined within.

I managed to implement it as intended and wanted to extend its use to helping me set the devices wallpaper. This turned out to be pretty complicated but I managed with the following code in my MainActivity.java:

public class MainActivity extends Activity {
private final ImageDownloader mDownload = new ImageDownloader();
public static Bitmap bima=null;
public static WallpaperManager wm;

in onCreate:

 wm = WallpaperManager.getInstance(getApplicationContext());

method defined in the MainActivity.Java:

public static void setbima(Bitmap bimu) {

    try {wm.setBitmap(bimu);} catch (Exception e) {}

Then I used my function in the ImageDownloader class' AsyncTask's onPostExecute like this:


Making use of the bitmap that was being passed to there by the AsyncTask class.

With the WallpaperManager object I wanted to make use of that bitmap, because its function .setBitmap() accepts bitmaps and puts the system wallpaper to that bitmap.

I am just wondering if my way of achieving this is optimal or is there a simpler way to have achieved this? It seems a very far-fetched way of doing this and I wouldn't be surprised if it is as I am a beginner programmer.


The system you have in place is efficient in the sense that it offloads the network-based work on to an AsyncTask, and the callback updates the wallpaper.

You don't give the details of your ImageLoader AsyncTask, but you could neaten a few things up by putting them in to there... and it would look something like:

public ImageLoader extends AsyncTask<String, Void, BitMap> {

    private final Context context;

    ImageLoader(Context context) {
        this.context = context;

    public BitMap doInBackground(String urlpath) {
        // set the bitmap... do the work.
        return bitmap;

    public void PostExecute(BitMap result) {
        try {
        } catch (....) {


With the above task, it becomes a simple thing to do in the onCreate of the MainActivity:

new ImageDownloader(getApplicationContext()).execute(url);

That's the way to make it neat....

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.