HarmonyOS选取并拷贝文件失败

代码如下:

import picker from '@ohos.file.picker'; 
import fs from '@ohos.file.fs'; 
@Entry 
@Component 
struct Index { 
  build() { 
    Row() { 
      Column() { 
        Button("copyFile") 
          .onClick(async () => { 
            try { 
              const photoSelectOptions = new picker.PhotoSelectOptions(); 
              photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE; 
              photoSelectOptions.maxSelectNumber = 1; 
              const photoPicker = new picker.PhotoViewPicker(); 
              const photoSelectResult = await photoPicker.select(photoSelectOptions); 
              const uri = photoSelectResult.photoUris[0]; 
              const url = getContext().cacheDir + '/image.jpg'; 
              await fs.copyFile(uri, url, 0); 
              console.info("copyFile succeed"); 
            } catch (error) { 
              console.info("copyFile failed", error); 
            } 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}
HarmonyOS
2024-09-09 11:41:37
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

参考实现demo:

async function example01() { 
  try { 
    let photoSelectOptions = new picker.PhotoSelectOptions(); 
    photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.VIDEO_TYPE || picker.PhotoViewMIMETypes.IMAGE_TYPE; 
    photoSelectOptions.maxSelectNumber = 5; 
    let photoPicker = new picker.PhotoViewPicker(); 
    photoPicker.select(photoSelectOptions).then((photoSelectResult: picker.PhotoSelectResult) => { 
      console.info('PhotoViewPicker.select successfully, photoSelectResult uri: ' + JSON.stringify(photoSelectResult.photoUris[0])); 
      const url = getContext().cacheDir + '/image.jpg'; 
      fs.copyFile(photoSelectResult.photoUris[0], url, 0); 
    }).catch((err: BusinessError) => { 
      console.error('PhotoViewPicker.select failed with err: ' + JSON.stringify(err)); 
    }); 
  } catch (error) { 
    let err: BusinessError = error as BusinessError; 
    console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err)); 
  } 
}
分享
微博
QQ
微信
回复
2024-09-09 15:16:21
相关问题
HarmonyOS 文件拷贝示例
32浏览 • 1回复 待解决
HarmonyOS 相册文件拷贝
34浏览 • 1回复 待解决
HarmonyOS 沙箱文件拷贝
67浏览 • 1回复 待解决
HarmonyOS拷贝拷贝
36浏览 • 1回复 待解决
HarmonyOS 图片资源选取
462浏览 • 1回复 待解决
HarmonyOS 文件预览失败
22浏览 • 1回复 待解决
HarmonyOS 文件预览功能失败
31浏览 • 1回复 待解决
HarmonyOS 读取本地RawFile文件失败
39浏览 • 1回复 待解决
HarmonyOS下载文件失败返回
511浏览 • 1回复 待解决
HarmonyOS web加载沙箱文件失败
32浏览 • 1回复 待解决
HarmonyOS 加载raw文件内容失败
54浏览 • 1回复 待解决
HarmonyOS pixelmap深拷贝问题
663浏览 • 1回复 待解决