HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Image-downloader/wallpaper setter

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
downloaderwallpaperimagesetter

Problem

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.

Solution

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 {

    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....

Code Snippets

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 (....) {
            ....
        }
    }

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

Context

StackExchange Code Review Q#21635, answer score: 5

Revisions (0)

No revisions yet.