HarmonyOS 标准化数据通路共享文件,文件在接收方无法访问

在进行使用UDMF进行文件共享时,数据提供方已经正常配置一个txt文件,但是数据接收方接收的时候可以再data.getRecords里面找到数据的URL,file://com.example.ipcclient/data/storage/el2/base/haps/entry/files/data.txt,但是检查文件却不存在。下面附上文件提供方和接收方的代码:

数据提供方代码:

import { fileIo, fileUri } from '@kit.CoreFileKit';
import { util } from '@kit.ArkTS';
import { unifiedDataChannel } from '@kit.ArkData';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct UDMFPage {
  @State message: string = 'Hello World';
  filePath: string = getContext().filesDir + "/data.txt";

  build() {
    Row() {
      Column() {
        Button('写入文件').onClick(() => {
          fileIo.open(this.filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE).then((file) => {
            fileIo.writeSync(file.fd, new util.TextEncoder().encodeInto('你好张三'))
          })
        }).width(86).height(36)
        Button('共享').onClick(() => {
          this.shareFiles()
        }).width('56vp').height(36)
      }
      .width('100%')
    }
    .height('100%')
  }
  shareFiles(){
    let fileUrlData: string = fileUri.getUriFromPath(this.filePath);
    let plainText = new unifiedDataChannel.File();
    plainText.uri = fileUrlData;
    let unifiedData = new unifiedDataChannel.UnifiedData(plainText);
    let options: unifiedDataChannel.Options = {
      intention: unifiedDataChannel.Intention.DATA_HUB
    }

    try {
      unifiedDataChannel.insertData(options, unifiedData, (err, data) => {
        if (err === undefined) {
          console.info(`Succeeded in inserting data. key = ${data}`);
        } else {
          console.error(`Failed to insert data. code is ${err.code},message is ${err.message} `);
        }
      });
    } catch (e) {
      let error: BusinessError = e as BusinessError;
      console.error(`Insert data throws an exception. code is ${error.code},message is ${error.message} `);
    }
  }
}
import { fileIo, fileUri, ReadOptions } from '@kit.CoreFileKit';
import { util } from '@kit.ArkTS';
import { unifiedDataChannel, uniformTypeDescriptor } from '@kit.ArkData';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct UDMFPage {
  @State message: string = 'Hello World';
  filePath: string = getContext().filesDir + "/data.txt";

  build() {
    Row() {
      Column() {
        Button('读取共享数据').onClick(() => {
          this.readFiles()
        }).width('86vp').height(56)
      }
      .width('100%')
    }
    .height('100%')
  }

  readFiles() {
    // key: 'file://com.example.ipcclient/data/storage/el2/base/haps/entry/files/data.txt'
    let options: unifiedDataChannel.Options = {
      intention: unifiedDataChannel.Intention.DATA_HUB
    }

    try {
      unifiedDataChannel.queryData(options, (err, data) => {
        if (err === undefined) {
          console.info(`Succeeded in querying data. size = ${data.length}`);
          for (let i = 0; i < data.length; i++) {
            let records = data[i].getRecords();
            for (let j = 0; j < records.length; j++) {
              if (records[j].getType() === uniformTypeDescriptor.UniformDataType.FILE) {
                let text = records[j] as unifiedDataChannel.File;
                console.info(`文件URL:${i + 1}.${text.uri}`);
                if (fileIo.accessSync(text.uri)) {
                  let fileInfo = fileIo.openSync(text.uri)
                  let buf = new ArrayBuffer(2048);
                  let readOptions: ReadOptions = {
                    offset: 0,
                    length: 2048
                  };
                  let readLen = fileIo.readSync(fileInfo.fd, buf, readOptions)
                  while (readLen > 0) {
                    console.log("data:" + util.TextDecoder.create('utf-8', { ignoreBOM: true })
                      .decodeWithStream(new Uint8Array(buf)))
                  }
                }else{
                  console.debug("文件不存在")
                }
              }
            }
          }
        } else {
          console.error(`Failed to query data. code is ${err.code},message is ${err.message} `);
        }
      });
    } catch (e) {
      let error: BusinessError = e as BusinessError;
      console.error(`Insert data throws an exception. code is ${error.code},message is ${error.message} `);
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
HarmonyOS
2024-12-27 15:13:16
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

关于UnifiedData的定义存在问题,统一数据对象UnifiedData在UDMF数据通路中具有全局唯一URI标识,其定义为udmf://intention/bundleName/groupId,其中各组成部分的含义分别为:

  1. udmf: 协议名,表示使用UDMF提供的数据通路。

  2. intention: UDMF已经支持的数据通路类型枚举值,对应不同的业务场景。

  3. bundleName: 数据来源应用的包名称。

  4. groupId: 分组名称,支持批量数据分组管理。

// 统一数据对象的URI
let options: unifiedDataChannel.Options = {
  key: 'udmf://DataHub/com.ohos.test/0123456789'
};
  • 1.
  • 2.
  • 3.
  • 4.
  1. 相关文档及开发步骤参考链接如下:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/unified-data-channels-V5#标准化数据通路的定义和实现

  1. 标准化数据定义与描述参考链接如下:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-data-uniformtypedescriptor-V5

实现数据通路共享 即数据提供方创建一个统一数据对象并插入到UDMF的公共数据通路中,数据访问方查询存储在UDMF公共数据通路中的全量统一数据对象;

  1. 全局唯一URI标识定义为下面各位部分的组成,获取到udmf: 协议名、intention、bundleName、groupId: 分组名称,可以通过拼接字符串生成全局唯一URI标识;

  2. 官方示例中,全局唯一URI标识的定义是以 udmf 即协议名开头的;

  3. 数据访问方拿到的是 数据提供方 存储在UDMF公共数据通路中的全量统一数据对象UnifiedData;

  4. 全局唯一URI标识在更新已写入UDMF的公共数据通路的数据会用到(即key的值),共享的是目标数据UnifiedData

相关方法链接参考:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-data-unifieddatachannel-V5#unifieddatachannelinsertdata

分享
微博
QQ
微信
回复
2024-12-27 18:29:09
相关问题
标准化数据通路UDMF传输限制问题
1299浏览 • 1回复 待解决
HarmonyOS 标准化数据通路删除异常
776浏览 • 1回复 待解决
HarmonyOS Web无法访问指定html
982浏览 • 1回复 待解决
Nginx无法访问localhost怎么回事?
3360浏览 • 1回复 待解决
HarmonyOS三方适配库文档无法访问
1226浏览 • 1回复 待解决
HarmonyOS 资源文件无法跨模块访问
965浏览 • 1回复 待解决