Resources.openRawResource() issue Android
Asked Answered
A

2

15

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

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.

Applicatory answered 2/6, 2009 at 12:9 Comment(1)
has any body faced the same problem as i have used openFileInput method but it checks the device path not the project path ....Applicatory
S
38

Yes, you should be able to use openRawResource to copy a binary across from your raw resource folder to the device.

Based on the example code in the API demos (content/ReadAsset), you should be able to use a variation of the following code snippet to read the db file data.

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

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();
Suazo answered 3/6, 2009 at 17:3 Comment(0)
R
0

InputStream.available has severe limitations and should never be used to determine the length of the content available for streaming.

http://developer.android.com/reference/java/io/FileInputStream.html#available(): "[...]Returns an estimated number of bytes that can be read or skipped without blocking for more input. [...]Note that this method provides such a weak guarantee that it is not very useful in practice."

You have 3 solutions:

  1. Go through the content twice, first just to compute content length, second to actually read the data
  2. Since Android resources are prepared by you, the developer, hardcode its expected length
  3. Put the file in the /asset directory and read it through AssetManager which gives you access to AssetFileDescriptor and its content length methods. This may however give you the UNKNOWN value for length, which isn't that useful.
Recitative answered 3/2, 2012 at 17:2 Comment(3)
Hi, is this still valid nowadays?Alpestrine
@Alpestrine The guarantee mention has been since removed from the API, but I'd still avoid the method in practice.Recitative
Ok, and about the 3), why may that returns unknown?Alpestrine

© 2022 - 2024 — McMap. All rights reserved.