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 //源端把需要共享给对端的文件名列表发送给对端

相关推荐

  1. 27 设备流转使用心得

    2024-07-14 17:30:02       27 阅读
  2. 总结心得:各设计模式使用场景

    2024-07-14 17:30:02       53 阅读
  3. clickhouse使用心得

    2024-07-14 17:30:02       47 阅读
  4. kylin使用心得

    2024-07-14 17:30:02       31 阅读
  5. draw.io使用心得

    2024-07-14 17:30:02       32 阅读
  6. draw.io使用心得

    2024-07-14 17:30:02       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 17:30:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 17:30:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 17:30:02       58 阅读
  4. Python语言-面向对象

    2024-07-14 17:30:02       69 阅读

热门阅读

  1. 删除矩阵中0所在行 matlab

    2024-07-14 17:30:02       21 阅读
  2. 英文论文审稿2

    2024-07-14 17:30:02       22 阅读
  3. 半导体行业术语Part01

    2024-07-14 17:30:02       28 阅读
  4. go语言 中 new能初始化哪些类型?

    2024-07-14 17:30:02       18 阅读
  5. 深度学习早停(early stop)训练策略

    2024-07-14 17:30:02       20 阅读
  6. 昇思训练营打卡第二十五天(RNN实现情感分类)

    2024-07-14 17:30:02       17 阅读
  7. 使用Scikit-Learn决策树:分类问题解决方案指南

    2024-07-14 17:30:02       13 阅读
  8. return promise 为undefined原因

    2024-07-14 17:30:02       18 阅读
  9. UNION 和 UNION ALL

    2024-07-14 17:30:02       21 阅读
  10. vue2一个计时器的功能

    2024-07-14 17:30:02       24 阅读