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 图片资源选取
235浏览 • 1回复 待解决
HarmonyOS pixelmap深拷贝问题
386浏览 • 1回复 待解决
HarmonyOS下载文件失败返回
280浏览 • 1回复 待解决
HarmonyOS 数据库拷贝后无法读取
78浏览 • 1回复 待解决
怎样保持文件允许用户拷出来?
1797浏览 • 1回复 待解决
HarmonyOS 如何对数组进行深拷贝
43浏览 • 1回复 待解决
HarmonyOS ArkWeb组件是否支持深拷贝
304浏览 • 2回复 待解决
应用内组件截图保存到用户文件
857浏览 • 1回复 待解决
如何将so文件导入,完成调用
2479浏览 • 1回复 待解决
dlopen打开.so文件使用md5加密
645浏览 • 1回复 待解决