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} `);
    }
  }
}
        HarmonyOS
      
        赞
        
 收藏 0
 回答 1
 
        待解决
        
相关问题
 HarmonyOS 标准化数据通路共享文件问题 
1156浏览  • 1回复 待解决
HarmonyOS 标准化数据通路删除异常 
1069浏览  • 1回复 待解决
标准化数据通路UDMF传输限制问题 
1732浏览  • 1回复 待解决
HarmonyOS UDMF标准化数据通路如何进行鉴权? 
1198浏览  • 1回复 待解决
#鸿蒙通关秘籍#如何利用标准化数据通路管理鸿蒙应用的数据共享? 
1133浏览  • 1回复 待解决
HarmonyOS 标准化数据通路中如何删除指定的数据内容 
1029浏览  • 1回复 待解决
#鸿蒙通关秘籍#如何在鸿蒙应用中使用标准化数据通路实现数据更新? 
2213浏览  • 1回复 待解决
#鸿蒙学习大百科#标准化数据通路的工作逻辑是什么? 
1269浏览  • 1回复 待解决
#鸿蒙学习大百科#如何理解UDMF提供的公共数据通路? 
1241浏览  • 1回复 待解决
创建的library module 无法访问资源文件咋回事啊? 
3518浏览  • 1回复 待解决
跳转登录,无法访问此网址,应用开发文档,无法访问 
11447浏览  • 4回复 待解决
HarmonyOS Web无法访问指定html 
1246浏览  • 1回复 待解决
#鸿蒙学习大百科#标准化数据类型是怎么分类的? 
1682浏览  • 2回复 待解决
标准化数据定义如何适配联邦学习场景下的数据特征对齐与隐私保护需求? 
550浏览  • 0回复 待解决
Nginx无法访问localhost怎么回事? 
3639浏览  • 1回复 待解决
请问RabbitMQ在CentOS安装后无法访问web页面怎么解决 
3858浏览  • 1回复 待解决
HarmonyOS 无法访问https://repo.harmonyos.com/ohpm/ 
1357浏览  • 1回复 待解决
如何通过标准化元数据确保数据一致性?是否支持动态扩展元数据字段? 
522浏览  • 0回复 待解决
HarmonyOS三方适配库文档无法访问 
1624浏览  • 1回复 待解决
HarmonyOS  资源文件无法跨模块访问 
1432浏览  • 1回复 待解决
HarmonyOS 跨设备发送文件,无法发送和接收 
1347浏览  • 1回复 待解决
HarmonyOS ArkTS无法访问带特殊字符的对象属性 
1203浏览  • 1回复 待解决
HarmonyOS 网络超时无法访问contentcenter-drcn.dbankcdn.cn 
2573浏览  • 1回复 待解决
#鸿蒙通关秘籍#如何在鸿蒙系统中接收他人共享的文件? 
1341浏览  • 1回复 待解决
HarmonyOS 无法接收推送数据 
1245浏览  • 1回复 待解决





















关于UnifiedData的定义存在问题,统一数据对象UnifiedData在UDMF数据通路中具有全局唯一URI标识,其定义为udmf://intention/bundleName/groupId,其中各组成部分的含义分别为:
udmf: 协议名,表示使用UDMF提供的数据通路。
intention: UDMF已经支持的数据通路类型枚举值,对应不同的业务场景。
bundleName: 数据来源应用的包名称。
groupId: 分组名称,支持批量数据分组管理。
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/unified-data-channels-V5#标准化数据通路的定义和实现
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-data-uniformtypedescriptor-V5
实现数据通路共享 即数据提供方创建一个统一数据对象并插入到UDMF的公共数据通路中,数据访问方查询存储在UDMF公共数据通路中的全量统一数据对象;
全局唯一URI标识定义为下面各位部分的组成,获取到udmf: 协议名、intention、bundleName、groupId: 分组名称,可以通过拼接字符串生成全局唯一URI标识;
官方示例中,全局唯一URI标识的定义是以 udmf 即协议名开头的;
数据访问方拿到的是 数据提供方 存储在UDMF公共数据通路中的全量统一数据对象UnifiedData;
全局唯一URI标识在更新已写入UDMF的公共数据通路的数据会用到(即key的值),共享的是目标数据UnifiedData
相关方法链接参考:
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-data-unifieddatachannel-V5#unifieddatachannelinsertdata