5

i am creating a gallery app , my first app and this is my code

    Bitmap bmd = BitmapFactory.decodeStream(is);

    try{
        getApplicationContext().setWallpaper(bmd);
    }catch(IOException e){
        e.printStackTrace();
    }

The above code sets wallpaper But the wallpaper gets cropped or zoomed after it got set !! Is there any modification i can do in the above code so that i can set wallpaper without zooming or cropping when it is set !!!!

Plzzzz help me out !! Thanks in advance :-)

anticafe
  • 6,816
  • 9
  • 43
  • 74
Kalpana
  • 59
  • 1
  • 3
  • You want to set the bitmap size equal to the size of the device's display, or you want to achieve the parallax effect? – forcelain Jan 27 '14 at 06:13

2 Answers2

4

I am late to reply this. Hope it helps you and those who visits your question:

In your case, try to adjust your picture to device size by somewhat like this:

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels << 1; // best wallpaper width is twice screen width

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, width, height);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap decodedSampleBitmap = BitmapFactory.decodeFile(path, options);

WallpaperManager wm = WallpaperManager.getInstance(this);
try {
    wm.setBitmap(decodedSampleBitmap);
} catch (IOException e) {
    Log.e(TAG, "Cannot set image as wallpaper", e);
}

If above code doesn't work, do a small modification:

...
WallpaperManager wm = WallpaperManager.getInstance(this);
try {
    wm.setBitmap(decodedSampleBitmap);
    wm.suggestDesiredDimensions(width, height);
} catch (IOException e) {
    Log.e(TAG, "Cannot set image as wallpaper", e);
}

And the method calculateInSampleSize:

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

and remember to add permissions:

<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
0

Easiest way is to expand your wallpaper picture to a nn square, put your content to the center of the new picture. If your deive is w1024h600, your wallpaper should be 10241024. If your deive is w600h1024, your wallpaper should also be 1024*1024.

Wayne
  • 21
  • 2