如何从NDK读取文件,使用AssetFileDescriptor的偏移量、长度和文件路径?

问题描述 投票:0回答:1

我知道可以从AssetFileDescriptor内容中读取文件,比如偏移量和长度。我通过以下方式将数据传递给JNI。

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")
}

现在,在NDK端,我如何读取文件?

extern "C" JNICALL JNIEXPORT
void Java_package_TheClass_ndkJNIBind(
        JNIEnv* env,
        jobject __unused,
        jstring path,
        jint fileLength,
        jint fileOffset
) {
    // TODO: ??????
}
android android-ndk
1个回答
0
投票

https:/developer.android.comndkreferencegroupasset

使用 AAsset_seek 将文件指针重新定位到一个偏移量上,而 AAsset_read 来读取字节。因为看起来你的活动是一个Java活动,所以用 AAssetManager_fromJava 来获取资产管理人对象。

另一个示例 此处.

© www.soinside.com 2019 - 2024. All rights reserved.