Sushant Sharma
Sushant Sharma

Reputation: 54

Setting Bitmap as wallpaper

I want to display an image from URL and then set that displayed image as wallpaper on the phone at button click..Currently it is displaying the image but nothing happens when the button is pressed to save it..Here is the code i am using..Kindly find out what i am doing wrong..thanks in advance

set = (Button) findViewById(R.id.abcd);

new DownloadImageTask((ImageView) findViewById(R.id.imageView1)).execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");

set.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        Thread th = new Thread(){
            public void run(){
            WallpaperManager image = WallpaperManager.getInstance(getApplicationContext());
            try{
                image.setBitmap(photo);
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
            }
        };
        th.start();
    }

});

}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap photo = null;
        try {
InputStream in = new java.net.URL(urldisplay).openStream();
babes = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
        }
        return photo;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
} 

Upvotes: 1

Views: 2877

Answers (2)

Kalai.G
Kalai.G

Reputation: 1610

Here is an example

    private class InitialTask extends AsyncTask<String, Void, Void> {

    private final ProgressDialog dialog = new ProgressDialog(
            ImageActivity.this);

    // can use UI thread here
    protected void onPreExecute() {
        this.dialog.setMessage("Loading...");
        this.dialog.show();

    }

    // automatically done on worker thread (separate from UI thread)
    protected Void doInBackground(final String... a) {

        try {
            Thread.sleep(100);
            try {
                int newpos = position;

                /*
                 * ImageId=Id[newpos];
                 * ImageID=Integer.parseInt(ImageId.toString()); saveimg=
                 * img[newpos];
                 */
                String url1 = imageUrls[newpos];
                // imageView.setImageBitmap(BitmapFactory.decodeResource(getResources(),
                // ids[position]));
                try {
                    URL ulrn = new URL(url1);
                    HttpURLConnection con = (HttpURLConnection) ulrn
                            .openConnection();
                    InputStream is = con.getInputStream();
                    bmp = BitmapFactory.decodeStream(is);

                    int widthPx = getWindowManager().getDefaultDisplay()
                            .getWidth();
                    int heightPx = getWindowManager().getDefaultDisplay()
                            .getHeight();
                    bmp = Bitmap.createScaledBitmap(bmp, widthPx, heightPx,
                            true);

                } catch (Exception ex) {

                }
            } catch (Exception e) {
                int i = 0;
            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    // can use UI thread here
    protected void onPostExecute(final Void unused) {

        if (null != bmp) {
            imageView = new ImageView(cxt);
            imageView.setImageBitmap(bmp);
            BitmapDrawable drawable = (BitmapDrawable) imageView
                    .getDrawable();
            Bitmap bitmap = drawable.getBitmap();
            try {
                WallpaperManager myWallpaperManager = WallpaperManager
                        .getInstance(getApplicationContext());
                myWallpaperManager.setBitmap(bitmap);
                Toast.makeText(ImagePagerActivity.this, "Wallpaper set",
                        Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Toast.makeText(ImagePagerActivity.this,
                        "Error setting Wallpaper", Toast.LENGTH_SHORT)
                        .show();
            }

        }

        if (this.dialog.isShowing()) {
            this.dialog.dismiss();

        }

    }

}

Upvotes: 0

Marco
Marco

Reputation: 1334

Have you added this permission?

<uses-permission android:name="android.permission.SET_WALLPAPER" />

Upvotes: 4

Related Questions