7

I'm trying to read a simple text file from my native code.

  1. I placed file.txt under assets folder
  2. In my activity I'm creating asset manager: assetManager = getAssets(); Then I'm passing the assetManager to my native method and(as in the native audio example):

    AAssetManager* mgr = AAssetManager_fromJava(env, assetManager);
    AAsset* asset = AAssetManager_open(mgr, "file.txt", AASSET_MODE_UNKNOWN);
    AAssetManager* mgr = AAssetManager_fromJava(env, assetManager);
    
    off_t start, length;
    
    int fd = AAsset_openFileDescriptor(asset, &start, &length);   
    

The problem is that fd is smaller than 0!!!

Can anyone help on this?

rgettman
  • 176,041
  • 30
  • 275
  • 357
nmnir
  • 568
  • 1
  • 10
  • 24

1 Answers1

7

AAsset_openFileDescriptor will work only with files that are not compressed (like mp3,jpg,png, etc...). It's written in documentation (asset_manager.h header file):

/**
 * Open a new file descriptor that can be used to read the asset data.
 *
 * Returns < 0 if direct fd access is not possible (for example, if the asset is
 * compressed).
 */
int AAsset_openFileDescriptor(AAsset* asset, off_t* outStart, off_t* outLength);

Use either AAsset_read or AAsset_getBuffer.

krsteeve
  • 1,794
  • 4
  • 19
  • 29
Mārtiņš Možeiko
  • 12,733
  • 2
  • 45
  • 45
  • 1
    Thanks. So it compressed text file by default? – nmnir Mar 01 '12 at 13:12
  • Sorry for recomennting but I realy need filedescriptor so what is the way using AAsset_read or AAsset_getBuffer to un compres the file? – nmnir Mar 01 '12 at 13:20
  • apk file is zip file. So most of files it contains (including ones in assets subdirectory) are compressed. Only specific files like mp3, jpeg, png and few other are not compressed. – Mārtiņš Možeiko Mar 01 '12 at 13:32
  • 1
    For compressed files you can not get file descriptor. If you are using ant task to build apk, then you can modify aapt build task to exculde files with specific extensions from compression: http://stackoverflow.com/questions/6809651/documentation-for-aapt-element-in-ant-script Of course, then your apk file will be bigger. – Mārtiņš Možeiko Mar 01 '12 at 13:38
  • Thanks. Is there a way to config the apk builder of eclipse? – nmnir Mar 01 '12 at 13:53
  • if you have an .obj file for a model you will need to make sure to not be compressed, this happened to me as well – FrickeFresh Feb 25 '17 at 13:27