将文件资产打包到apk中?

时间:2012-03-24 03:36:06

标签: android file cmusphinx

我的应用程序在设备上时需要使用一些文件。 现在,我只能通过复制这些文件并将其直接粘贴到设备中来完成此操作 通过手动使用电脑。

有没有办法将这些文件打包或复制到apk中,并在设备上安装时,和 程序将粘贴它们以自动指定设备中的文件夹

我要打包的文件是狮身人面像语音识别的模型

感谢。

1 个答案:

答案 0 :(得分:4)

您可以将文件放在项目的/ assets目录中,然后将其直接构建到apk中,然后使用getAssets()方法访问该文件以复制它或任何您需要的文件Context 1}}运行时的对象:

InputStream input = context.getAssets().open("your_file_name");
String outPath = "/some/path/your_file_name";
OutputStream output = new FileOutputStream(outPath);

// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
    output.write(buffer, 0, length);
}

// Close the streams
output.flush();
output.close();
input.close();
相关问题