0

Im trying to set my bitmap image as a wallpaper!

Im currently using this code to get it done.

            WallpaperManager wpm = WallpaperManager.getInstance(this);

            float minH =  wpm.getDesiredMinimumHeight();
            float minW =  wpm.getDesiredMinimumWidth();
            Log.d("seb", minH + " = Min Height");
            Log.d("seb", minW + " = Min Width");
            targetBitmap = Bitmap.createScaledBitmap(targetBitmap,(int)minW, (int)minH, false);         
            wpm.setBitmap(targetBitmap);

It works! The phone automatically resizes the bitmap to fit the screen, but no mather how small my bitmap is, it's always cropped horizontally and scaled up.

Does anyone know how to fix this?

(One fix would be to put a black border around and have them cropped instead of the actual picture, though Im guessing there is a better alternative )

EDIT

This is original picture

this is original picture in code.

The following picture is what I mean with cropped when set to wallpaper:

Cropped picture

And this will be in the same way even if i resize the image since the system automatically enlarges the picture to fit the whole screen

Sebastian
  • 2,698
  • 2
  • 17
  • 26

1 Answers1

0

Ok, so after our discussion in "comments", here is a solution which can work.

Try that:

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

    int width = bm.getWidth();

    int height = bm.getHeight();

    float scaleWidth = ((float) newWidth) / width;

    float scaleHeight = ((float) newHeight) / height;

// create a matrix for the manipulation

    Matrix matrix = new Matrix();

    // resize the bit map

    matrix.postScale(scaleWidth, scaleHeight);

// recreate the new Bitmap

    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

    return resizedBitmap;

}

Try it and if you have the same (or another) problem, let me know and we will try to fix it.

Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265