【问题标题】:How can I read a file from NDK using offset, length and file path from AssetFileDescriptor?如何使用 AssetFileDescriptor 的偏移量、长度和文件路径从 NDK 读取文件?
【发布时间】:2020-06-11 17:07:12
【问题描述】:

我知道可以从 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】:

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

    使用AAsset_seek 将文件指针重新定位到偏移量,并使用AAsset_read 读取字节。由于您的活动看起来像是 Java 活动,因此请使用 AAssetManager_fromJava 获取 Asset Manager 对象。

    另一个示例here

    【讨论】:

      猜你喜欢
      • 2021-07-24
      • 1970-01-01
      • 1970-01-01
      • 2017-08-20
      • 2012-03-29
      • 1970-01-01
      • 2014-07-26
      • 2023-03-04
      • 1970-01-01
      相关资源
      最近更新 更多