3

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 construct 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!

iterator
  • 49
  • 5
  • Although this is an old post, there's a nice [workaround here](http://stackoverflow.com/questions/14801876/android-accessing-file-from-internal-storage-using-randomaccessfile). The resource file is first copied from assets to the cache directory. From there it can be opened with a RandomAccessFile. – Francis Apr 17 '13 at 19:51

3 Answers3

0

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.

Pentium10
  • 204,586
  • 122
  • 423
  • 502
0

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. :-)

mreichelt
  • 12,359
  • 6
  • 56
  • 70
  • The problem with skip is that it is not guaranteed to work (according to the Java docs), and the bytes up to the skip point are read into an internal buffer, which consumes memory. For now I'll be using another approach and not worrying about the random access. I did see somewhere that Google might unzip the .apk at some future point which would remedy this. – iterator Sep 16 '10 at 04:09
0

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.

broot
  • 21,588
  • 3
  • 30
  • 35