5

I am developing an app which picks an image from the gallery and then sets that image as the wallpaper. But here problem is that only part of image is set as wallpaper not the whole image, but I want to set the whole image as the wallpaper. can you please tell me how that can be done ???

Here is my code...

public class Scaleimage extends Activity {
    /** Called when the activity is first created. */

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String FileName;
        File file = new File("/sdcard/pictures");   
        File[] imageFiles = file.listFiles( );
        if(imageFiles.length > 0 ) {
            FileName = imageFiles[0].getName();
        final WallpaperManager wallpaperManager = WallpaperManager.getInstance(getBaseContext());   
        Bitmap myBitmap =  BitmapFactory.decodeFile("/sdcard/pictures" + "/" + FileName); 

        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int height = displayMetrics.heightPixels;
        int width = displayMetrics.widthPixels << 1;
        myBitmap = Bitmap.createScaledBitmap(myBitmap,width, height, true);
        try {
            wallpaperManager.setBitmap( myBitmap);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
    }
}
Swayam
  • 16,294
  • 14
  • 64
  • 102

2 Answers2

7

Set wallpaper size to your image size:

WallpaperManager wm = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
wm.setBitmap(bitmap);
wm.suggestDesiredDimensions(w, h);

and remember to add permissions:

<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
xtr
  • 5,870
  • 1
  • 21
  • 23
  • 2
    Note the message from the documentation on the `suggestDesiredDimensions()` method: **"Note developers, who don't seem to be reading this. This is for home screens to tell what size wallpaper they would like. Nobody else should be calling this! Certainly not other non-home-screen apps that change the wallpaper. Those apps are supposed to retrieve the suggested size so they can construct a wallpaper that matches it."** – Dzhuneyt Sep 26 '14 at 12:09
1

Pass width height to wallpaper manager like this :

final WallpaperManager wallpaperManager = (WallpaperManager)getSystemService(
                Context.WALLPAPER_SERVICE);    

Bitmap myBitmap = Bitmap.createScaledBitmap(Const.cropped_bitmap, wallpaperManager.getDesiredMinimumWidth(), wallpaperManager.getDesiredMinimumHeight(), true);
wallpaperManager.suggestDesiredDimensions(wallpaperManager.getDesiredMinimumWidth(), wallpaperManager.getDesiredMinimumHeight());

try {

    wallpaperManager.setBitmap(myBitmap);
    Toast.makeText(CropImageActivity.this, CropImageActivity.this.getString(R.string.wallpaper_has_been_set), 0).show();
} catch (IOException e) {
    e.printStackTrace();
    Toast.makeText(CropImageActivity.this, "Wallpaper not set", 0).show();
}

Do not forget to add permission :

<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
Milan Hirpara
  • 534
  • 4
  • 18