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 创建相机预览失败
369浏览 • 1回复 待解决
使用DevEco Studio时Java预览提示错误
9504浏览 • 1回复 待解决
使用预览器实现多端预览
830浏览 • 1回复 待解决
HarmonyOS PDF 预览问题
194浏览 • 1回复 待解决
如何使用预览接口进行文件预览
290浏览 • 1回复 待解决
卡片能否通过预览器进行预览
484浏览 • 1回复 待解决
DevEco Studio预览器支持实时预览吗?
6003浏览 • 1回复 待解决
camera 获取预览数据
1643浏览 • 1回复 待解决
HarmonyOS Web组件加载pdf预览
57浏览 • 1回复 待解决
Hyperlink的onTouch预览报错
1759浏览 • 1回复 待解决
webview是否支持预览pdf
1029浏览 • 1回复 待解决
HarmonyOS filePreview使用问题
191浏览 • 1回复 待解决
HarmonyOS使用Web组件预览PDF和图片
462浏览 • 1回复 待解决
HarmonyOS 摄像头预览画面方向错误
178浏览 • 1回复 待解决
HarmonyOS 需要一个图片预览组件
89浏览 • 1回复 待解决