27 设备流转使用心得 三

前两部分参考心得 25 26 

分布式文件传输

1 源端

1 获取分布式文件路径 读取文件 写入分布式文件

2 对端

1 通过应用沙箱获取分布式文件路径 读取文件路径 与状态数据绑定

2 绑定之后UI渲染

Index

Row({space:8}){
  //用户当前选中的所有图片
  ForEach(this.photos, (p:string)=>{
    Image(p).width(50).height(50)
  })
  //添加图片按钮
  Image('/image/ic_public_add.svg').width(50).padding(4).borderRadius(6).backgroundColor('#f0f0f0').clickEffect({level:ClickEffectLevel.MIDDLE, scale:0.6})
    .onClick(async _=>{
      //拉起“图片选择器”,让用户选择图片文件
      let p = new picker.PhotoViewPicker()
      let result = await p.select({MIMEType: picker.PhotoViewMIMETypes.IMAGE_TYPE})
      if (result.photoUris.length > 0) {
        //this.photos = result.photoUris        //新选择的图片会覆盖之前已选图片
        this.photos.push(...result.photoUris)   //新选择的图片追加到已选图片的尾部
      }
    })
}

onCreat()

/**** 对端读取源端数据方式3:从分布式文件目录下读取源端共享的文件 *****/
let list = want.parameters?.photoList as string[]
let fullFileNames:string[] = [] //拷贝到对端应用文件目录下的完整文件名
if(list && list.length>0){
  let filesDir = this.context.filesDir  //应用文件保存目录
  let distributedFilesDir = this.context.distributedFilesDir //分布式文件保存目录
  list.forEach((f:string)=>{
    this.copyFile(distributedFilesDir+f, filesDir+f)  //把分布式共享文件目录下的临时文件拷贝到应用目录下去
    // fullFileNames.push(filesDir+f)  //应用文件目录下的路径名 不能 直接作为Image的src显示出来
    fullFileNames.push( fileUri.getUriFromPath(filesDir+f) )  //把应用文件目录下的路径名转换为URI,形如:file://cn.tedu.myapp14/data/storage/...png
  })
  this.storage.set('photoList', fullFileNames) //UIAbility与UI共享完整文件名
}

this.context.restoreWindowStage(this.storage)//在目的端恢复窗口舞台,并把UIAbility中的storage传给UI

onContinue()

/***** 源端给对端传递数据方式3: 分布式文件共享 *****/
//把用户当前在UI中选中的图片拷贝到“分布式共享文件目录”下
let fileList = this.storage.get('photoList') as string[]
if(fileList.length>0){
  let base = this.context.distributedFilesDir //分布式文件所在目录,类似于:/data/storage/el2/distributedfiles
  let newFileNames:string[] = []  //重命名后的文件名列表
  fileList.forEach((src:string)=>{
    let dest = '/'+Date.now()+Math.floor(Math.random()*9000+1000)+src.substring(src.lastIndexOf('.'))  //新建的随机文件名,命名规则:/+时间戳+四位随机数+原始后缀名
    this.copyFile(src, base+dest)  //在源端把图库中的文件路径拷贝到分布式文件目录下,就可以同步给对端
    console.log('--源端:把用户选择的文件拷贝到了分布式文件目录', src, base+dest)
    newFileNames.push(dest)
  })
  console.log('--源端:即将共享给对端的分布式文件名:',JSON.stringify(newFileNames))
  wantParam['photoList'] = newFileNames //源端把需要共享给对端的文件名列表发送给对端

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值