4

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

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

我要打包的文件是sphinx语音识别的模型

谢谢。

4

1 回答 1

4

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

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();
于 2012-03-24T03:49:44.033 回答