HarmonyOS鸿蒙Next中fs.open(this.imageurl, fs.OpenMode.READ_ONLY)打开文件报错13900001

HarmonyOS鸿蒙Next中fs.open(this.imageurl, fs.OpenMode.READ_ONLY)打开文件报错13900001 我在尝试使用 fs.open(this.imageurl, fs.OpenMode.READ_ONLY) 打开例如地址是 file://media/Photo/1433/IMG_1722394290_1419/IMG_1419.jpg 的图库图片时报错 open file failed with error message: Operation not permitted, error code: 13900001,是需要什么权限吗?

这个问题是偶现的,有时候报错,有时候不报错。

代码如下:

fs.open(this.imageurl, fs.OpenMode.READ_ONLY).then((file: fs.File) => {
  console.info("file fd: " + file.fd);
  let imageSourceApi: image.ImageSource =
    image.createImageSource(file.fd); // 通过PixelMap进行编码。compressedImageData为打包获取到的图片文件流。
  imageSourceApi.createPixelMap().then((pixelmap: image.PixelMap) => {
    console.log('cwidth cheight ' + this.canvasRadio);
    pixelmap.getImageInfo().then(async (imageinfo: image.ImageInfo) => {
      if (this.imgBitMap != null) {
        this.canvas = new MyCanvas(this.canvasContext, this.imgBitMap, pixelmap);
      }
      this.canvasRadio = imageinfo.size.width / imageinfo.size.height
      console.log('cwidth cheight ' + this.canvasRadio);
    })
  }).catch((error: BusinessError) => {
    console.error('widthFailed to create pixelMap object through image decoding parameters.');
  })
  fs.closeSync(file);
}).catch((err: BusinessError) => {
  console.error("widthopen file failed with error message: " + err.message + ", error code: " + err.code);
});

更多关于HarmonyOS鸿蒙Next中fs.open(this.imageurl, fs.OpenMode.READ_ONLY)打开文件报错13900001的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

楼主解决了吗,遇到同样的问题,发现好像是云端照片的uri打开会error,本地的不会

更多关于HarmonyOS鸿蒙Next中fs.open(this.imageurl, fs.OpenMode.READ_ONLY)打开文件报错13900001的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


试试

import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { fileIo } from '@kit.CoreFileKit';
import { image } from '@kit.ImageKit';
import { buffer } from '@kit.ArkTS';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  @State pixelMap: PixelMap | undefined = undefined
  @State resultBase64Str: string  = ''

  getPhotoUrl() {
    try {
      let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
      PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
      PhotoSelectOptions.maxSelectNumber = 1;
      let photoPicker = new photoAccessHelper.PhotoViewPicker();
      photoPicker.select(PhotoSelectOptions).then(async (PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => {
        const selectUri = PhotoSelectResult.photoUris[0];
        console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' + JSON.stringify(PhotoSelectResult));
        let file = fileIo.openSync(selectUri, fileIo.OpenMode.READ_ONLY)
        let imageSource: image.ImageSource = image.createImageSource(file.fd);
        this.pixelMap = await imageSource.createPixelMap();
        this.encodeToBase64()
      }).catch((err: BusinessError) => {
        console.error(`PhotoViewPicker.select failed with err: ${err.code}, ${err.message}`);
      });
    } catch (error) {
      let err: BusinessError = error as BusinessError;
      console.error(`PhotoViewPicker failed with err: ${err.code}, ${err.message}`);
    }
  }

  encodeToBase64() {
    let packOpts : image.PackingOption = { format:"image/jpeg", quality:98 };
    let imagePackerApi = image.createImagePacker();
    imagePackerApi.packing(this.pixelMap, packOpts).then( (data : ArrayBuffer) => {
      let base64Str = buffer.from(data).toString('base64')
      this.resultBase64Str="data:image/png;base64,"+base64Str
      console.log('resultBase64Str = ', this.resultBase64Str)
    }).catch((error : BusinessError) => {
      console.error('Failed to pack the image. And the error is: ' + error);
    })
  }

  build() {
    Column() {
      Text(this.message)
        .id('LocalStoragePageHelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
        .onClick(() => {
          this.getPhotoUrl()
        })
      Image(this.resultBase64Str)
        .width('100%')
        .height(300)
        .objectFit(ImageFit.Contain)
    }
    .height('100%')
    .width('100%')
  }
}

如果不行,请更新DevEco Studio到最新版本,然后再次尝试,新版DevEco Studio下载地址如下 :

https://developer.huawei.com/consumer/cn/download/

对于问题一(拿不到图像的宽度以及高度):因为使用的imageurl保存的是生成图片的url,并不是选择的图片的url,一开始通过用户点击获得的权限是“选择的图片”的权限,而没有“保存的图片”的权限,可以在ImageEditorPagew.ets中将imageurl修改成固定的被选择的图片的路径,会发现可以拿到对应的信息。

在HarmonyOS鸿蒙Next中,使用fs.open(this.imageurl, fs.OpenMode.READ_ONLY)打开文件时,报错13900001通常表示文件路径无效或文件不存在。该错误码属于文件系统操作中的常见错误,可能与以下几个原因相关:

  1. 文件路径错误:this.imageurl可能未正确指向目标文件,路径可能拼写错误或未包含完整路径信息。
  2. 文件不存在:目标文件在指定路径中不存在,或文件已被删除。
  3. 权限问题:应用可能没有权限访问指定路径下的文件。
  4. 路径格式问题:路径可能不符合鸿蒙系统要求的格式,例如未使用正确的URI格式。

可以检查this.imageurl的值,确保路径正确且文件存在。如果路径正确,检查应用权限配置,确保应用具有访问文件的权限。

错误码13900001通常表示文件路径或权限问题。请检查以下几点:

  1. 文件路径:确保this.imageurl是有效的文件路径,且路径格式正确。

  2. 文件存在:确认文件确实存在于指定路径。

  3. 权限:确保应用具有读取该文件的权限,检查config.json中的权限配置,确保已声明ohos.permission.READ_MEDIA或其他相关权限。

  4. 路径前缀:如果使用相对路径,可能需要加上internal://file://等前缀。

如果问题仍未解决,建议使用fs.access先检查文件可访问性,再进行操作。

回到顶部