android - 从 MODE_PRIVATE FileNotFoundException 保存和读取文件
问题描述
我在资产文件夹中有一个 apk 文件。我想从 MODE_PRIVATE 复制 apk 文件然后读取它。我编写了写入和读取函数但无法读取我的文件。我有 FileNotFoundException 这是我的函数
private void writeFilePrivate() {
AssetManager assetManager = getAssets();
InputStream in;
FileOutputStream fos;
try {
in = assetManager.open("test.apk");
fos = openFileOutput("tmp.apk", Context.MODE_PRIVATE);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
fos.write(buffer, 0, read);
}
in.close();
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private File readFileFromPrivate() {
int ch;
StringBuffer fileContent = new StringBuffer("");
FileInputStream fis;
try {
fis = openFileInput("test.apk");
try {
while ((ch = fis.read()) != -1)
fileContent.append((char) ch);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String data = new String(fileContent);
File file = new File(data);
return file;
}
我在日志中有这个错误
java.io.FileNotFoundException: /data/data/com.mypackage.music/files/test.apk: open failed: ENOENT (No such file or directory)
我究竟做错了什么 ?
解决方案
推荐阅读
- docker - 如何拥有多个版本的pdfinfo?
- sql - 将数据从 S3 导入 Redshift 时的 SUPER 数据类型解析
- java - 如何将不同类的三个线程同步到一起
- javascript - 如何在另一个公会中担任角色并在新公会 Discord.js v.12 中获得排名
- python - 获取挂起的更改列表
- mysql - TypeORM 抛出 QueryFailedError ER_TABLE_EXISTS_ERROR: MySQL 中已经存在表'courses'
- android - 堆分析插件和 Android Gradle 插件 4.2+
- react-native - 播放 Lottie 库的特定动画并跳过 React Native 中的其他部分
- math - 半对数刻度图上的直线通过两点的方程
- python - 变量作为后缀?