HarmonyOS 华为系统分享面板分享到app的uri无读取权限

华为系统分享面板分享到app的uri无读取权限,例如:

根据文档要求,通过如下api获取的数据,无读取权限

systemShare.getSharedData(want)
  .then((data: systemShare.SharedData) => {
    data.getRecords().forEach((record: systemShare.SharedRecord) => {
      //这里拿到的uri无权限,如:file://media/Photo/1/IMG_1721727644_000/IMG_20240723_173904.jpg
    });
  })
  .catch((error: BusinessError) => {
    this.context.terminateSelf();
  });
HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple
  1. module.json5
{
  "module": {
  "name": "entry",
  "type": "entry",
  "description": "$string:module_desc",
  "mainElement": "EntryAbility",
  "deviceTypes": [
  "phone",
  "tablet",
  "2in1"
  ],
  "deliveryWithInstall": true,
  "installationFree": false,
  "pages": "$profile:main_pages",
  "abilities": [
  {
    "name": "EntryAbility",
  "srcEntry": "./ets/entryability/EntryAbility.ets",
  "description": "$string:EntryAbility_desc",
  "icon": "$media:layered_image",
  "label": "$string:EntryAbility_label",
  "startWindowIcon": "$media:startIcon",
  "startWindowBackground": "$color:start_window_background",
  "exported": true,
  "skills": [
    {
      "entities": [
      "entity.system.home",
      ],
      "actions": [
      "action.system.home",
      "ohos.want.action.sendData"
      ],
      "uris": [
      {
        "scheme": "file",
      "utd": "general.image",
      "maxFileSupported": 9
      },
      {
        "scheme": "file",
      "utd": "general.video",
      "maxFileSupported": 9
      }
      ]
    }
    ]
  }
  ],
  "extensionAbilities": [
  {
    "name": "EntryBackupAbility",
  "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets",
  "type": "backup",
  "exported": false,
  "metadata": [
    {
      "name": "ohos.extension.backup",
    "resource": "$profile:backup_config"
    }
    ],
  }
  ]
}
}
  1. EntryAbility.ets
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';


interface IStorage {
  ShareUIAbilityWant: Want;
}

export default class EntryAbility extends UIAbility {
  private storage: LocalStorage = new LocalStorage();
  private windowClass: window.Window | undefined = undefined;

  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
    this.storage.setOrCreate('ShareUIAbilityWant', want);
  }

  onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onNewWant');
    this.storage.setOrCreate('ShareUIAbilityWant', want);
    this.windowClass?.loadContent('pages/Index', this.storage);
  }

  onDestroy(): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage): void {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
    windowStage.loadContent('pages/Index', this.storage);
    this.windowClass = windowStage.getMainWindowSync();
  }

  onWindowStageRestore(windowStage: window.WindowStage): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageRestore');
  }

  onWindowStageDestroy(): void {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground(): void {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground(): void {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
}
  1. Index.ets
import { systemShare } from '@kit.ShareKit';
import { uniformTypeDescriptor as utd } from '@kit.ArkData';
import { Want } from '@kit.AbilityKit';

const TAG = 'TARGET_INDEX'

// 获取共享的storage实例
let storage = LocalStorage.getShared();

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  @State imageUri: string = '';
  private want: Want | undefined = storage.get<Want>('ShareUIAbilityWant');

  onPageShow(): void {
    this.want = storage.get<Want>('ShareUIAbilityWant');
    systemShare.getSharedData(this.want).then((data: systemShare.SharedData) => {
      let records: systemShare.SharedRecord[] = data.getRecords();
      records.forEach(async (record: systemShare.SharedRecord) => {
        switch (record.utd) {
          case utd.UniformDataType.IMAGE:
            record.uri && (this.imageUri = record.uri);
            break;
          default:
            break;
        }
      });
    })
  }

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })

      if (this.imageUri) {
        Image(this.imageUri)
          .width(200)
          .height(200)
      }
    }
    .height('100%')
    .width('100%')
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 系统分享文件
68浏览 • 1回复 待解决
应用调用系统分享框失败
432浏览 • 1回复 待解决
HarmonyOS如何使用系统分享图片、文件
520浏览 • 1回复 待解决
华为JS API如何唤起分享
4603浏览 • 1回复 待解决
HarmonyOS 应用文件分享-读写权限咨询
424浏览 • 1回复 待解决
分享沙箱文件,应用可分享单个文件
1094浏览 • 1回复 待解决
getDefaultCellularDataSlotIdSync实战分享
256浏览 • 0回复 待解决
分享一下你鸿蒙系统使用感受吧
4268浏览 • 1回复 待解决
HarmonyOS 分享功能如何实现?
225浏览 • 1回复 待解决
有没有权限说明清单,分享一下啊?
4382浏览 • 1回复 待解决