Resources.openRawResource() issue Android

后端 未结 2 2069
终归单人心
终归单人心 2020-11-30 10:21

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

2条回答
  •  天涯浪人
    2020-11-30 10:48

    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.

提交回复
热议问题