Pack files assets into apk?
Question
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
The files that I want to pack are the model for sphinx speech recognition
Thank.
La solution
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:
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();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow