Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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

ImageDownloader.java

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());
    mDownload.download(url,imageView);

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:

MainActivity.setbima(bitmap);

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.

share|improve this question
add comment

1 Answer 1

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 {
            WallpaperManager.getInstance(context).setBitmap(result);
        } 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....

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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