• 【ARKUI】HarmonyOS 如何设置桌面壁纸


     参考资料

    壁纸

    Context模块

    api讲解

    wallpaper.setWallpaper

    setWallpaper(source: string | image.PixelMap, wallpaperType: WallpaperType): Promise

    将指定资源设置为指定类型的壁纸。

    需要权限:ohos.permission.SET_WALLPAPER

    系统能力: SystemCapability.MiscServices.Wallpaper

    参数:

    参数名类型必填说明
    sourcestring |PixelMapJPEG或PNG文件的Uri路径,或者PNG格式文件的位图。
    wallpaperTypeWallpaperType壁纸类型。

    返回值:

    类型说明
    Promise调用成功则返回是返回设置的结果,调用失败则返回error信息。

    示例:

    1. // source类型为string
    2. let wallpaperPath = "/data/data/ohos.acts.aafwk.plrdtest.form/files/Cup_ic.jpg";
    3. wallpaper.setWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
    4. console.log(`success to setWallpaper.`);
    5. }).catch((error) => {
    6. console.error(`failed to setWallpaper because: ` + JSON.stringify(error));
    7. });

    代码实现

    • 准备工作

      准备一张图片放在rawfile文件目录,如下图所示

      cke_1280.png

      cke_2464.png

    • 申请权限

      在config.json注册如下权限代码如下

      1. "reqPermissions": [
      2. {
      3. "name": "ohos.permission.READ_USER_STORAGE"
      4. },
      5. {
      6. "name": "ohos.permission.WRITE_USER_STORAGE"
      7. },
      8. {
      9. "name": "ohos.permission.SET_WALLPAPER"
      10. }
      11. ],

      在mainAbility注册申请权限,并把权限写入指定文件夹目录下,代码如下

      1. package com.newdemo.myapplication;
      2. import ohos.aafwk.content.Operation;
      3. import ohos.ace.ability.AceAbility;
      4. import ohos.aafwk.content.Intent;
      5. import ohos.global.resource.RawFileEntry;
      6. import ohos.global.resource.Resource;
      7. import ohos.security.SystemPermission;
      8. import java.io.File;
      9. import java.io.FileOutputStream;
      10. import java.io.IOException;
      11. public class MainAbility extends AceAbility {
      12. @Override
      13. public void onStart(Intent intent) {
      14. String[] permissions = {
      15. "ohos.permission.WRITE_USER_STORAGE",
      16. "ohos.permission.READ_USER_STORAGE",
      17. " ohos.permission.SET_WALLPAPER"
      18. };
      19. requestPermissionsFromUser(permissions, 0);
      20. super.onStart(intent);
      21. writeToDisk( "entry/resources/rawfile/result.png","/result.png");
      22. }
      23. private void writeToDisk(String rawFilePath, String resultpath) {
      24. String externalFilePath = getFilesDir() +resultpath ;
      25. File file = new File(externalFilePath);
      26. if (file.exists()) {
      27. return;
      28. }
      29. RawFileEntry rawFileEntry = getResourceManager().getRawFileEntry(rawFilePath);
      30. try (FileOutputStream outputStream = new FileOutputStream(new File(externalFilePath))) {
      31. Resource resource = rawFileEntry.openRawFile();
      32. // cache length
      33. byte[] cache = new byte[1024];
      34. int len = resource.read(cache);
      35. while (len != -1) {
      36. outputStream.write(cache, 0, len);
      37. len = resource.read(cache);
      38. }
      39. } catch (IOException exception) {
      40. }
      41. }
      42. }
    • Ets语言实现

      在Ets绘画一个text文本内容为:设置壁纸,然后实现点击功能,代码如下

      1. import image from '@ohos.multimedia.image'
      2. import wallpaper from '@ohos.wallpaper';
      3. import featureAbility from '@ohos.ability.featureAbility'
      4. @Entry
      5. @Component
      6. struct Mypage {
      7. public onclick() {
      8. var context = featureAbility.getContext();
      9. context.getFilesDir((err, data) => {
      10. if (err) {
      11. console.error('Operation failed. Cause: ' + JSON.stringify(err));
      12. return;
      13. }
      14. let wallpaperPath =data+"/result.png";
      15. wallpaper.setWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => {
      16. if (error) {
      17. console.error(`failed to setWallpaper because: ` + JSON.stringify(error));
      18. return;
      19. }
      20. console.log(`success to setWallpaper.`);
      21. });
      22. });
      23. }
      24. build() {
      25. Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      26. Text('设置壁纸')
      27. .fontSize(50)
      28. .fontWeight(FontWeight.Bold)
      29. .onClick(this.onclick.bind(this))
      30. }
      31. .width('100%')
      32. .height('100%')
      33. }
      34. }

    运行效果

    fe01f050f7cf8e0d3fb3b077649e1b09_416x949.gif%40900-0-90-f.gif

     欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

  • 相关阅读:
    被Gartner列入十大战略技术趋势的“行业云”,不再是个伪命题?
    Q-Tester 3.2:适用于开发、生产和售后的诊断测试软件
    【STM32】入门(五):串口TTL、RS232、RS485
    python多进程(类成员函数、log在多进程中使用、自己维护进程池)
    【C++程序员必修第一课】C++基础课程-04:变量、常量和指针
    SSM前后端分离技术
    微信小程序 --- 常用样式和组件
    java面试题之找出1至1000以内的质数
    BGP学习笔记
    k8s 1.22.3使用持久化卷之存储类StorageClass+NFS pv动态供应
  • 原文地址:https://blog.csdn.net/weixin_44708240/article/details/126359844