8

I have kept some files in assets folder of android application.

Now i compile that application and get apk. [when i extract that apk my files are there in assets folder]

Now i install that apk in mobile device

and in That application has some code in c programing with JNI interface.

Now my fopen() call in c programming for that file get failed.

fopen("myfile","r");

I know while installing apk android copy assets file to /data/data/com.packagename/something

So does anyone knows that path so i can give it in fopen()

or is there any other method to open that file in C programming.


If i keep those files in sdcard folder and access like fopen("/sdcard/myfile","r"); then it works file. But i want to keep those files in assets folder only

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
  • 4
    "I know while installing apk android copy assets file to /data/data/com.packagename/something" -- and your proof of this is, what, exactly? AFAIK, assets are not files on the device, but remain packaged in the APK. – CommonsWare Mar 23 '16 at 12:44

2 Answers2

6

You can use the asset manager to access assets in JNI code. It's part of NDK.

http://developer.android.com/ndk/reference/group___asset.html

Something like:

EXPORT_API void LoadAsset(char* filename, jobject assetManager){
    AAssetManager* mgr = AAssetManager_fromJava(env, assetManager);
    AAsset* asset = AAssetManager_open(mgr, filename, AASSET_MODE_BUFFER);
    AAsset_read(asset, ...);
}

And then in Java something like:

LoadAsset("myfile", getContext().getAssetManager());
kichik
  • 33,220
  • 7
  • 94
  • 114
0

One way I've found is to write a test line in the code, something like:

System.out.println("Working Directory = " +
              System.getProperty("user.dir"));

Now, if you know where your assets are stored by Android, /data/data/com.packagename/assets/file.png, for example, then you will know the reletive or absolute path to the assets. I hope this works for you, it helped me!

Cody Richardson
  • 157
  • 3
  • 16