Resources.openRawResource()问题的Android [英] Resources.openRawResource() issue Android
问题描述
我在 RES /生/
文件夹中的数据库文件。我打电话 Resources.openRawResource()
的文件名如 R.raw.FileName
,我得到的输入流,但我有在设备中的另一个数据库文件,所以数据库我用的数据库中的内容复制到设备:
I have a database file in res/raw/
folder. I am calling Resources.openRawResource()
with the file name as R.raw.FileName
and I get an input stream, but I have an another database file in device, so to copy the contents of that db to the device db I use:
BufferedInputStream bi = new BufferedInputStream(is);
和FileOutputStream中,但我得到一个异常的数据库文件已损坏。我该如何进行?
我尝试使用文件
和的FileInputStream
和路径为 / RES /生读取文件/文件名
,但也不能正常工作。
and FileOutputStream, but I get an exception that database file is corrupted. How can I proceed?
I try to read the file using File
and FileInputStream
and the path as /res/raw/fileName
, but that also doesn't work.
推荐答案
是的,你应该能够使用 openRawResource
从您的原始资源文件夹复制跨二进制设备
Yes, you should be able to use openRawResource
to copy a binary across from your raw resource folder to the device.
InputStream ins = getResources().openRawResource(R.raw.my_db_file);
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
int size = 0;
// Read the entire resource into a local byte buffer.
byte[] buffer = new byte[1024];
while((size=ins.read(buffer,0,1024))>=0){
outputStream.write(buffer,0,size);
}
ins.close();
buffer=outputStream.toByteArray();
你的文件的副本现在应该在缓冲存
,这样你就可以使用的FileOutputStream
保存缓冲区到一个新的文件中。
A copy of your file should now exist in buffer
, so you can use a FileOutputStream
to save the buffer to a new file.
FileOutputStream fos = new FileOutputStream("mycopy.db");
fos.write(buffer);
fos.close();
这篇关于Resources.openRawResource()问题的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!