0

I know that is possible to read a file from AssetFileDescriptor content like offset and length. I'm passing to JNI data in the following way:

val resources = context.get()!!.resources
val file = resources.openRawResourceFd(rawFileId) // example: R.raw.you_file
val fileOffset = file.startOffset.toInt()
val fileLength = file.length.toInt()

try {
    file.parcelFileDescriptor.close()
} catch (e: IOException) {
    Log.e("error", "Couldn't close")
}

try {
    this.id = ndkJNIBind(
        context.get()!!.packageResourcePath, fileLength, fileOffset)
} catch (e: UnsatisfiedLinkError) {
    Log.e("error", "Couldn't do ndkJNIBind")
}

Now, in NDK side how can I read the file?

extern "C" JNICALL JNIEXPORT
void Java_package_TheClass_ndkJNIBind(
        JNIEnv* env,
        jobject __unused,
        jstring path,
        jint fileLength,
        jint fileOffset
) {
    // TODO: ??????
}
Ráfagan
  • 2,165
  • 2
  • 15
  • 22

1 Answers1

0

https://developer.android.com/ndk/reference/group/asset

Use AAsset_seek to reposition the file pointer to an offset, and AAsset_read to read the bytes. Since it looks like your activity is a Java one, use AAssetManager_fromJava to get the Asset Manager object.

Another sample here.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281