Android 如何打开assets包中的后缀为.tflite的文件报错提示FileNotFound
时间: 2024-05-07 13:21:26 浏览: 7
在 Android 中打开 assets 文件夹中的 .tflite 文件,可以使用以下代码:
```
private MappedByteBuffer loadModelFile() throws IOException {
AssetFileDescriptor fileDescriptor = getAssets().openFd("your_model.tflite");
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
```
其中的 "your_model.tflite" 需要替换成你自己的 .tflite 文件名。如果你的文件名带有文件夹路径,需要在文件名前加上文件夹路径。
如果你遇到了 "java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed" 的错误提示,可能是因为你的 .tflite 文件被压缩了。你可以尝试将文件的压缩方式从 "gzip" 改为 "uncompressed"。具体方法是在 build.gradle 文件中的 android 节点下添加以下代码:
```
aaptOptions {
noCompress "tflite"
}
```
这样就可以避免压缩 .tflite 文件。