开发者

How to get a path to resources?

开发者 https://www.devze.com 2023-01-15 07:28 出处:网络
I\'d like to use the RandomAccessFile class to access a file that ships with my application. However, that class only accepts a String which is the path to the file. If I place my file somewhere like

I'd like to use the RandomAccessFile class to access a file that ships with my application. However, that class only accepts a String which is the path to the file. If I place my file somewhere like the assets directory, how do I con开发者_如何学运维struct a path to that file? I know I can use the getAssets methods to read up resources, but those only return InputStreams which cannot be seeked.

Thanks!


I don't know if you just miss-readed but I find a plenty of methods available that takes String as input.

Check the documentation of AssetManager you will find methods like:

final String[]   list(String path)
Return a String array of all the assets at the given path.

final InputStream    open(String fileName, int accessMode)
Open an asset using an explicit access mode, returning an InputStream to read its contents.
final InputStream    open(String fileName)
Open an asset using ACCESS_STREAMING mode.

So at least there is one list method you can use, and two open methods.


The problem with resources is you do not get any file names - at least until Android 2.2. So in fact you can not use the class RandomAccessFile. But I think you can try to use the methods of InputStream instead: reset() and skip(long) should do the trick. :-)


Android resources aren't intended to be accessed directly as files. Actually they're usually stored in .apk, so they aren't files at all - just zipped data.

You could do basic manipulations on InputStream object. If you need something more advanced, then you could read whole file to a memory buffer or copy it to a real file somewhere on data partition or sdcard.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号