Android set image as wallpaper

前端 未结 2 601
醉梦人生
醉梦人生 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:53

    Try below code in ImagePagerActivity, i tested below code and it is working.

        // fetch bitmap from view
    public static Bitmap getBitmapFromView(View view) {
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view
                .getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);
        Drawable bgDrawable = view.getBackground();
        if (bgDrawable != null)
            bgDrawable.draw(canvas);
        else
            // if we unable to get background drawable then we will set white color as wallpaper
            canvas.drawColor(Color.WHITE);
        view.draw(canvas);
        return returnedBitmap;
    }
    
    public void setWall(int i) {
        WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
        try {
            // below line of code will set your current visible pager item to wallpaper
            // first we have to fetch bitmap from visible view and then we can pass it to wallpaper
            myWallpaperManager.setBitmap(getBitmapFromView(pager.getChildAt(1)));
    
            // below line of code will set input stream data directly to wallpaper
            // myWallpaperManager.setStream(InputStream Data);
    
            // below line of code will set any image which is in the drawable folder 
            // myWallpaperManager.setResource(R.drawable.icon);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    It will set current visible pager's item view(if it is progress wheel or image).

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题