context.startAbilityForResult({
action:'ohos.want.action.imageCapture',
parameters:{
callBundleName:'com.hm.imageshow'
}
}, async (err, data) => {
if (err?.code === 0) {
const uri = (data?.want?.parameters as Record<string, Object>)['resourceUri']?.toString();
const fileName = getFileName(uri);
const fileType = fileName.lastIndexOf('.') > -1 ? fileName.substring(fileName.lastIndexOf('.')) : '';
const localId = `${dayjs().format('YYYYMMDDHHmmssSSS')}_${Global.getInstance().getPartIMEI()}${fileType}`;
const saveDir = `${SYSTEM_DIRECTORY_CAMERA}files/`;
const srcPath = uri.replace('file:/', '');
const destPath = `${saveDir}${localId}`;
if (!isFileExist(saveDir)) {
fs.mkdirSync(saveDir, true);
}
if (isSaveToAlbum) {
fs.copyFileSync(srcPath, destPath, 0);
} else {
fs.moveFileSync(srcPath, destPath, 0);
}
拍照或者选择照片获取到的路径为file://,无法拷贝到沙箱中,调用copyFileSync,moveFileSync均报错,如何解决
看下下面这个demo
fs.moveFileSync的用法:
其中srcPath 是:源文件的应用沙箱路径,destPath 是:目的文件的应用沙箱路径。