How to set image as wallpaper programmatically?

前端 未结 2 945
心在旅途
心在旅途 2021-01-12 21:43

I have been developing the application which need to set an image as wallpaper.

Code:

WallpaperManager m=WallpaperManager.getInstan         


        
2条回答
  •  庸人自扰
    2021-01-12 22:12

    File f = new File(Environment.getExternalStorageDirectory(), "1.jpg");
    String path = f.getAbsolutePath();
    File f1 = new File(path);
    
    if(f1.exists()) {
        Bitmap bmp = BitmapFactory.decodeFile(path);
        BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp);
        WallpaperManager m=WallpaperManager.getInstance(this);
    
        try {
            m.setBitmap(bmp);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } 
    

    Open Androidmanifest.xml file and add permission like..

    
    

    Try this and let me know what happen..

提交回复
热议问题