How to set image as wallpaper from the ImageView?

10,213

Solution 1

Step 1: Get the image attached to the ImageView.

Setp 2: Set that image as Wallpaper.

Step 3: Add permission in the AndroidManifest.xml to set wallpaper!

For step 1 check This answer!

imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();

For step 2:

WallpaperManager m=WallpaperManager.getInstance(this);

try {
    m.setBitmap(bmap);
} catch (IOException e) {
    e.printStackTrace();
}

For step 3: Include this permission too.

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

Tell me if that does not works for you!

Solution 2

This can be answered in a two parts.

The first would be to set the WallPaper:

WallpaperManager wallManager = WallpaperManager.getInstance(getApplicationContext());
try {
    wallManager.setBitmap(bmpImg);
    Toast.makeText(MainActivity.this, "Wallpaper Set Successfully!!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
    Toast.makeText(MainActivity.this, "Setting WallPaper Failed!!", Toast.LENGTH_SHORT).show();
}

The second part is kinda optional and would come in to picture only if you haven't set a Bitmap to your ImageView. In that case, you will need to do this step before setting up the WallPaper:

Bitmap bmpImg = ((BitmapDrawable)yourImageView.getDrawable()).getBitmap();
Share:
10,213
Muhammad Akram Khan
Author by

Muhammad Akram Khan

Updated on June 05, 2022

Comments

  • Muhammad Akram Khan
    Muhammad Akram Khan about 2 years

    I have an activity in which there are two Buttons and an ImageView. One button is to take image from the Camera application of the phone and set it to the ImageView, and other Button is to set that image as the Home screen wallpaper so i want the code how to set this image from the ImageView to the wallpaper???????

  • Khan
    Khan almost 9 years
    Hello there! May I know which is better way to set the wallpaper. I tried both answers and they are looking same in performance. Please tell me which is the better way?
  • Siddharth Lele
    Siddharth Lele almost 9 years
    @KhanSquare: Hard to say. Both are almost alike. Except that the other question has more votes. ;-)
  • Khan
    Khan almost 9 years
    But I'm using your method! You have more reputation than Shajeel Afjal So I'm going with experience. ;)
  • Siddharth Lele
    Siddharth Lele almost 9 years
    @KhanSquare. Honestly speaking, my reputation shouldn't be the deciding factor. Lets face it, the other answer does have more votes. And while I have never gotten around to testing his code, if you haven't noticed in difference in performance between the two, use my code. In which case, I am glad to have been of help. :-)
  • Stevie
    Stevie over 5 years
    we can not registe permission . <uses-permission android:name="android.permission.WRITE_SETTINGS" />