简体   繁体   English

将文件资产打包到apk中?

[英]Pack files assets into apk?

I have some files that need to use by my application when it is on the device. 我的应用程序在设备上时需要使用一些文件。 Right now, I can only done this by copy those files and paste it into device directly by using computer manually. 现在,我只能通过复制这些文件并通过手动使用计算机将其直接粘贴到设备中来完成此操作。

So is there any way to pack or copy these files into apk and when installing on the device, and The program will paste them to specify folder in the device automatically 那么有没有办法将这些文件打包或复制到apk中,并在设备上安装时,程序会将它们粘贴到设备中自动指定文件夹

The files that I want to pack are the model for sphinx speech recognition 我想要打包的文件是狮身人面像语音识别的模型

Thank. 谢谢。

You can place the files in the /assets directory of your project, which will build it directly into the apk, and then access the file to copy it or whatever you need using the getAssets() method on a Context object at runtime: 您可以将文件放在项目的/ assets目录中,这将直接将其构建到apk中,然后在运行时使用Context对象上的getAssets()方法访问该文件以复制它或任何您需要的文件:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM