Discuz! BBS

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2|回复: 1

将文件复制到私有目录

[复制链接]

395

主题

560

帖子

3306

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3306
发表于 昨天 18:54 | 显示全部楼层 |阅读模式
VoskHelper cn.unicoder.myapplication E 文件读取或发送失败: /data/user/0/cn.unicoder.myapplication/files/test16k.wav: open failed: ENOENT (No such file or directory

根据日志信息,问题在于测试文件不存在。/data/user/0/cn.unicoder.myapplication/files/test16k.wav 路径下的文件未找到。
问题分析
测试代码设置了文件路径为应用私有目录下的 test16k.wav
但该文件实际上并不存在,导致 VoskHelper.sendFile() 方法在尝试读取文件时抛出 FileNotFoundException
解决方案
需要将测试音频文件从 assets 目录复制到应用私有目录中:

  1. private String getTestWavFilePath() {
  2.     Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
  3.     String testFilePath = context.getFilesDir().getAbsolutePath() + "/test16k.wav";
  4.    
  5.     // 检查文件是否存在,如果不存在则从assets复制
  6.     File testFile = new File(testFilePath);
  7.     if (!testFile.exists()) {
  8.         try {
  9.             AssetManager assetManager = context.getAssets();
  10.             InputStream inputStream = assetManager.open("test16k.wav");
  11.             
  12.             FileOutputStream outputStream = new FileOutputStream(testFilePath);
  13.             byte[] buffer = new byte[1024];
  14.             int length;
  15.             while ((length = inputStream.read(buffer)) > 0) {
  16.                 outputStream.write(buffer, 0, length);
  17.             }
  18.             inputStream.close();
  19.             outputStream.close();
  20.             
  21.             Log.d(tag, "测试文件已复制到: " + testFilePath);
  22.         } catch (IOException e) {
  23.             Log.e(tag, "复制测试文件失败", e);
  24.         }
  25.     }
  26.    
  27.     return testFilePath;
  28. }
复制代码
记得在文件顶部添加导入语句:
  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
复制代码


回复

使用道具 举报

395

主题

560

帖子

3306

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3306
 楼主| 发表于 昨天 19:15 | 显示全部楼层
测试文件不存在,须从assets拷贝一份: 2025-10-25 19:08:46.868 24212-24236 VoskHelper...rationTest cn.unicoder.myapplication E 复制测试文件失败.

根据日志信息,问题在于从 assets 目录中找不到名为 test16k.wav 的文件。
问题分析
应用尝试从 assets 目录打开 test16k.wav 文件时抛出了 FileNotFoundException
这意味着在 assets 目录中没有这个文件
解决方案
确认文件存在: 确保 test16k.wav 文件存在于项目的 app/src/main/assets/ 目录中

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-10-26 08:18 , Processed in 0.012083 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表