java – Resources. Openrawresource () releases Android

I have a database file in RES / raw / folder I'm calling resources Openrawresource(), the file name is r.raw Filename, I get an input stream, but I have another database file in the device, so I want to copy the contents of the database to the device database I use:

BufferedInputStream bi = new BufferedInputStream(is);

And fileoutputstream, but I received an exception. The database file is corrupt What should I do? I tried to read the file using file and FileInputStream with the path / RES / raw / filename, but it didn't work

Solution

Yes, you should be able to use openraw resource to copy binaries from the original resource folder to the device

According to the sample code in the API demo (content / readasset), you should be able to read database file data using variants of the following code snippet

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,1024))>=0){
  outputStream.write(buffer,size);
}
ins.close();
buffer=outputStream.toByteArray();

A copy of your file should now exist in the buffer, so you can use fileoutputstream to save the buffer to a new file

FileOutputStream fos = new FileOutputStream("mycopy.db");
fos.write(buffer);
fos.close();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>