1***@qq.com
1***@qq.com
  • 发布:2023-10-23 14:19
  • 更新:2023-10-23 16:37
  • 阅读:831

uniapp如何将图片设置为手机壁纸?

分类:uni-app
2023-10-23 14:19 负责人:无 分享
已邀请:
喜欢技术的前端

喜欢技术的前端 - QQ---445849201

希望能够帮到你

setting() {  
                let url = 'https://ask.dcloud.net.cn/static/images/side/ask_zp_20230710.png';  
                // #ifdef APP-PLUS    
                let WallpaperManager = plus.android.importClass("android.app.WallpaperManager");  
                let Main = plus.android.runtimeMainActivity();  
                let wallpaperManager = WallpaperManager.getInstance(Main);  
                plus.android.importClass(wallpaperManager);  
                let BitmapFactory = plus.android.importClass("android.graphics.BitmapFactory");  
                uni.showLoading({  
                    title: '下载图片...',  
                    mask: true  
                });  
                uni.downloadFile({  
                    url: url,  
                    success: res => {  
                        let filePath = res.tempFilePath.replace('file://', '');  
                        filePath = plus.io.convertLocalFileSystemURL(filePath);  
                        let bitmap = BitmapFactory.decodeFile(filePath);  
                        try {  
                            wallpaperManager.setBitmap(bitmap); //设置壁纸    
                            uni.showToast({  
                                icon: 'none',  
                                title: '壁纸设置成功'  
                            });  
                        } catch (e) {  
                            // console.log(e,'err');    
                            uni.showToast({  
                                icon: 'none',  
                                title: '壁纸设置失败'  
                            })  
                        }  
                    },  
                    fail: err => {  
                        uni.showToast({  
                            icon: 'none',  
                            title: '壁纸设置失败'  
                        })  
                    },  
                    complete: () => {  
                        uni.hideLoading();  
                    }  
                });  
                // #endif    
                // #ifdef H5    
                uni.showToast({  
                    icon: 'none',  
                    title: '请在APP设置'  
                });  
                // #endif    
            }

要回复问题请先登录注册