#鸿蒙学习大百科#同一应用下如何获取其他设备的文件并拷贝到本机?

同一应用下如何获取其他设备的文件并拷贝到本机?

HarmonyOS
2024-10-22 16:03:33
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
后知后觉cy
import fs from '@ohos.file.fs';
import common from '@ohos.app.ability.common';
import fileUri from '@ohos.file.fileuri';
import { BusinessError } from '@ohos.base';

let context = getContext(this) as common.UIAbilityContext; // 获取设备B的UIAbilityContext信息
let pathDir: string = context.filesDir;
let distributedPathDir: string = context.distributedFilesDir;
// 待拷贝文件的目标沙箱路径
let filePath: string = pathDir + '/dest.txt';

// 获取目标路径uri
let destUri = fileUri.getUriFromPath(filePath);

// 获取分布式路径下的源文件
let srcUri: string = fileUri.getUriFromPath(distributedPathDir + '/src.txt');

// 定义拷贝回调
let progressListener: fs.ProgressListener = (progress: fs.Progress) => {
  console.info(`progressSize: ${progress.processedSize}, totalSize: ${progress.totalSize}`);
};
let options: fs.CopyOptions = {
  "progressListener" : progressListener
}

try {
  // 将分布式路径下的文件拷贝到其他沙箱路径下
  fs.copy(srcUri, destUri, options).then(()=>{
    console.info("Succeeded in copying of paste. ");
    console.info("src: " + srcUri + "dest: " + destUri); // file://com.example.myapplication/data/storage/el2/distributedfiles/src.txt
  }).catch((error: BusinessError) => {
    let err: BusinessError = error as BusinessError;
    console.info(`Failed to copy. Code: ${err.code}, message: ${err.message}`);
  })
} catch (error) {
  console.error(`Failed to copy. Code: ${error.code}, message: ${error.message}`);
}
分享
微博
QQ
微信
回复
2024-10-22 22:27:39
相关问题