HarmonyOS filePreview预览txt提示预览失败

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/preview-arkts-0000001716213002-V5#section1045913401474

export let mContext = getContext(this); 
 
Index.ets 
import { mContext } from '../entryability/EntryAbility'; 
 
//写文件 
function WriteFile() { 
  let filePath = mContext.filesDir + "/test.txt"; 
  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 
  let str: string = "hello, world"; 
  let writeLen = fs.writeSync(file.fd, str); 
  console.info("write data to file succeed and size is:" + writeLen); 
  fs.closeSync(file); 
} 
 
//遍历文件 
function GetFile() { 
  console.info('getDirectory mContext.filesDir:' + mContext.filesDir); 
  let listFileOption: ListFileOptions = { 
    recursion: true, 
    listNum: 0, 
  }; 
  let filenames = fs.listFileSync(mContext.filesDir, listFileOption); 
  console.info("listFile succeed, filenames.length:" + filenames.length); 
  for (let i = 0; i < filenames.length; i++) { 
    console.info("filename: %s", filenames[i]); 
  } 
} 
 
Button('WriteFile', { stateEffect: true, type: ButtonType.Capsule }) 
  .width('80%') 
  .height(40) 
  .margin(10) 
  .onClick(() => { 
    WriteFile() 
  }) 
Button('GetDirectory', { stateEffect: true, type: ButtonType.Capsule }) 
  .width('80%') 
  .height(40) 
  .margin(10) 
  .onClick(() => { 
    GetFile() 
  })EntryAbility.ets 
export let mContext = getContext(this); 
 
Index.ets 
import { mContext } from '../entryability/EntryAbility'; 
 
//写文件 
function WriteFile() { 
  let filePath = mContext.filesDir + "/test.txt"; 
  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 
  let str: string = "hello, world"; 
  let writeLen = fs.writeSync(file.fd, str); 
  console.info("write data to file succeed and size is:" + writeLen); 
  fs.closeSync(file); 
} 
 
//遍历文件 
function GetFile() { 
  console.info('getDirectory mContext.filesDir:' + mContext.filesDir); 
  let listFileOption: ListFileOptions = { 
    recursion: true, 
    listNum: 0, 
  }; 
  let filenames = fs.listFileSync(mContext.filesDir, listFileOption); 
  console.info("listFile succeed, filenames.length:" + filenames.length); 
  for (let i = 0; i < filenames.length; i++) { 
    console.info("filename: %s", filenames[i]); 
  } 
} 
 
Button('WriteFile', { stateEffect: true, type: ButtonType.Capsule }) 
  .width('80%') 
  .height(40) 
  .margin(10) 
  .onClick(() => { 
    WriteFile() 
  }) 
Button('GetDirectory', { stateEffect: true, type: ButtonType.Capsule }) 
  .width('80%') 
  .height(40) 
  .margin(10) 
  .onClick(() => { 
    GetFile() 
  })    let uiContext: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; 
let displayInfo: filePreview.DisplayInfo = { 
  x: 100, 
  y: 100, 
  width: 800, 
  height: 800 
}; 
let fileInfo: filePreview.PreviewInfo = { 
  uri: `file://${jsonObject['filePath'] as string}`, 
  mimeType: jsonObject['mimeType'] as string 
}; 
try { 
  await filePreview.openPreview(uiContext, fileInfo, displayInfo); 
} catch (err) { 
  console.error('[DownloadPlugin] openPreview failed, err = ' + err.message); 
}

使用上述代码进行txt文件预览是提示失败,文件通过判断是存在的。

HarmonyOS
2024-09-11 10:12:09
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

文件URI需要加入包名,例如:file://com.example.uc/data/storage/el2/base/haps/entry/files/IR240430142758048.txt。

分享
微博
QQ
微信
回复
2024-09-11 15:47:07
相关问题
HarmonyOS 创建相机预览失败
596浏览 • 1回复 待解决
使用DevEco Studio时Java预览提示错误
9819浏览 • 1回复 待解决
使用预览器实现多端预览
1095浏览 • 1回复 待解决
卡片能否通过预览器进行预览
603浏览 • 1回复 待解决
DevEco Studio预览器支持实时预览吗?
6288浏览 • 1回复 待解决
如何使用预览接口进行文件预览
478浏览 • 1回复 待解决
HarmonyOS PDF 预览问题
401浏览 • 1回复 待解决
HarmonyOS 相机-拍照之后预览
370浏览 • 1回复 待解决
HarmonyOS 如何实现图片预览
20浏览 • 1回复 待解决
HarmonyOS Web组件加载pdf预览
276浏览 • 1回复 待解决
camera 获取预览数据
1828浏览 • 1回复 待解决
HarmonyOS 相机预览是否支持放大缩小
31浏览 • 1回复 待解决
HarmonyOS 是否支持pdf、Word文件预览
339浏览 • 1回复 待解决
如何创建相机预览输出?
139浏览 • 0回复 待解决