0

I have tried to change wallpaper on android. but it jut shows a portion of the image. however if I lock the phone the lockscreen show the image perfectly scaled but only while the app is running, on exit the lockscreen shows the same as the background?

help!?

WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());



    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;



    Bitmap original = BitmapFactory.decodeResource(getResources(), R.drawable.day_cloudy);
    Bitmap resized = Bitmap.createScaledBitmap(original, width, height, true);





    try {
        wallpaperManager.clear();
        wallpaperManager.setWallpaperOffsetSteps(1, 1);
        wallpaperManager.suggestDesiredDimensions(resized.getWidth(), resized.getHeight());


        wallpaperManager.setBitmap(resized);
        Toast.makeText(this, "Wallpaper Set", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        e.printStackTrace();
    }

1 Answers1

0

You have to use

public void setStream (InputStream data) method:

InputStream ins = new URL("absolute/path/of/image").openStream();
WallpaperManager wpm = WallpaperManager.getInstance(context);
wpm.setStream(ins);

OR, if you have image URI then use:

WallpaperManager wpm = WallpaperManager.getInstance(context);
wpm.setResource(Uri.of.image);
tanmeet
  • 220
  • 2
  • 11