Android set image as wallpaper

前端 未结 2 607
醉梦人生
醉梦人生 2020-12-15 14:01

Please show me an example code on how to set image as wallpaper using Android WallpaperManager. I have shortened and edited my question. Hopefully you guys could understand

2条回答
  •  天涯浪人
    2020-12-15 14:56

    This might help you with your setbackground method...

    String imagePath = ""; // YOUR PATH HERE
    FileInputStream is = new FileInputStream(new File(imagePath));
    BufferedInputStream bis = new BufferedInputStream(is);
    Bitmap b = BitmapFactory.decodeStream(bis);
    Bitmap bitmapToUse = Bitmap.createScaledBitmap(b, parent.getWidth(), parent.getHeight(), true);
    b.recycle();
    
    if(!("").equals(imagePath)){
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
        Drawable wallpaperDrawable = wallpaperManager.getDrawable();
        wallpaperManager.setBitmap(bitmapToUse);
    }
    

    That should set your file to your wallpaper no problem.

    Can you be more specific on what you need for the "saveImage()"? Where are you trying to save from? Is it local storage? Or a website? More details please.

    [Edit] Updated code for clarity

    [Edit 2] To save images from a URL...

    File imageFile = new File("image.png"); // This is location AND file name, all i put here was the filename
    URL url = new URL("http://www.whatever.com/image.png");
    
                        Bitmap bmp = BitmapFactory.decodeStream(url.openConnection()
                                .getInputStream());
    
                        FileOutputStream out = new FileOutputStream(imageFile);
                        bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
    

    [Edit 3]

    The 'parent' is either your parent view (generally the view for the current activity). There are other ways to set this, the parent.width/height is how you're going to define how large the wallpaper image needs to be.

提交回复
热议问题