中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
同一应用下如何获取其他设备的文件并拷贝到本机?
微信扫码分享
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}`); }