Java Resources.openRawResource() 问题 Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/939170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Resources.openRawResource() issue Android
提问by Sam97305421562
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:
我在res/raw/
文件夹中有一个数据库文件。我Resources.openRawResource()
使用文件名调用asR.raw.FileName
并获得输入流,但我在设备中有另一个数据库文件,因此将该数据库的内容复制到我使用的设备数据库:
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.
和 FileOutputStream,但我得到一个异常,即数据库文件已损坏。我该如何继续?我尝试使用File
andFileInputStream
和路径读取文件/res/raw/fileName
,但这也不起作用。
采纳答案by Reto Meier
Yes, you should be able to use openRawResource
to copy a binary across from your raw resource folder to the device.
是的,您应该能够使用openRawResource
将二进制文件从原始资源文件夹复制到设备。
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.
根据 API 演示 (content/ReadAsset) 中的示例代码,您应该能够使用以下代码片段的变体来读取 db 文件数据。
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.
文件的副本现在应该存在于 中buffer
,因此您可以使用FileOutputStream
将缓冲区保存到新文件中。
FileOutputStream fos = new FileOutputStream("mycopy.db");
fos.write(buffer);
fos.close();
回答by TudorT
InputStream.available has severe limitations and should never be used to determine the length of the content available for streaming.
InputStream.available 有严重的限制,不应用于确定可用于流式传输的内容的长度。
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."
http://developer.android.com/reference/java/io/FileInputStream.html#available():“[...]返回可以读取或跳过而不阻塞更多输入的估计字节数。[. ..]请注意,此方法提供的保证如此薄弱,以至于在实践中并不是很有用。”
You have 3 solutions:
您有 3 个解决方案:
- Go through the content twice, first just to compute content length, second to actually read the data
- Since Android resources are prepared by you, the developer, hardcode its expected length
- 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.
- 遍历内容两次,第一次只是计算内容长度,第二次实际读取数据
- 由于 Android 资源是由开发者您准备的,因此对其预期长度进行硬编码
- 将文件放在 /asset 目录中并通过 AssetManager 读取它,这使您可以访问 AssetFileDescriptor 及其内容长度方法。然而,这可能会给你长度的 UNKNOWN 值,这不是那么有用。