Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fit the whole image on screen as wallpaper

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();
        }  
    }
}
like image 416
manisha marken Avatar asked Jul 10 '12 07:07

manisha marken


1 Answers

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"/>
like image 135
xtr Avatar answered Sep 27 '22 22:09

xtr