#鸿蒙学习大百科#如何通过分布式对象将资产同步到其他设备?

如何通过分布式对象将资产同步到其他设备?

HarmonyOS
2024-10-24 10:34:10
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
福娃泡泡

前提:已配置ohos.permission.DISTRIBUTED_DATASYNC并弹窗授权成功。

import distributedDataObject from '@ohos.data.distributedDataObject';
import commonType from '@ohos.data.commonType';

class People {
  name: string|undefined
  age: number |undefined= 0
  attachment: commonType.Asset | undefined

  constructor(name: string|undefined, age: number|undefined) {
    this.name = name
    this.age = age
  }
}
@Entry
@Component
struct Index {
  build() {
    Column() {
      Button("跨设备同步资产").onClick(() => {
        let attachment: commonType.Asset = {
          name: 'test_img.jpg',
          uri: 'file://com.example.myapplication/data/storage/el2/distributedfiles/dir/test_img.jpg',
          path: '/dir/test_img.jpg',
          createTime: '2024-01-02 10:00:00',
          modifyTime: '2024-01-02 10:00:00',
          size: '5',
          status: commonType.AssetStatus.ASSET_NORMAL
        }
        let people = new People("JASON",30)
        people.attachment = attachment
        let localObject: distributedDataObject.DataObject = distributedDataObject.create(getContext(this), people);
        // 发起端创建包含资产的分布式对象并加入组网
        localObject.setSessionId('123456');
      })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}
分享
微博
QQ
微信
回复
2024-10-24 16:26:41
相关问题