android - 应用程序在 native 代码中的外部存储路径

标签 android c++ android-ndk java-native-interface storage

我正在尝试为我的应用程序编写一个 native 库,以便我可以在 native 代码中执行所有文件操作。我读到 getExternalStorageDirectory() 给出目录的外部存储路径。

我的问题是如何在不将位置硬编码到某个字符串的情况下访问相同的内容? android ndk 中是否有任何函数可以在 C++ 代码中提供与 java 的 getExternalStorageDirectory() 相同的功能?

最佳答案

JNI 是您的 friend ,这并不太复杂,因为 getExternalStorageDirectory 是一个静态方法。此函数获取值,并将工作目录更改为该值,以备不时之需。

#include <jni.h>
#include <unistd.h> // chdir()
#include <sys/param.h> // MAXPATHLEN

// To call Java methods when running native code inside an Android activity,
// a reference is needed to the JavaVM.
static JavaVM *gJavaVM;

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
{
    gJavaVM = vm;
    return JNI_VERSION_1_6;
}

int cdToExtStorage(void) {

    // Make JNI calls to get the external storage directory, and cd to it.
    
    // To begin, get a reference to the env and attach to it.
    JNIEnv *env;
    int isAttached = 0;
    int ret = 0;
    jthrowable exception;
    if (((*gJavaVM)->GetEnv(gJavaVM, (void**)&env, JNI_VERSION_1_6)) < 0) {
        // Couldn't get JNI environment, so this thread is native.
        if (((*gJavaVM)->AttachCurrentThread(gJavaVM, &env, NULL)) < 0) {
            fprintf(stderr, "Error: Couldn't attach to Java VM.\n");
            return (-1);
        }
        isAttached = 1;
    }
    
    // Get File object for the external storage directory.
    jclass classEnvironment = (*env)->FindClass(env, "android/os/Environment");
    if (!classEnvironment) goto bailAndroid;
    jmethodID methodIDgetExternalStorageDirectory = (*env)->GetStaticMethodID(env, classEnvironment, "getExternalStorageDirectory", "()Ljava/io/File;"); // public static File getExternalStorageDirectory ()
    if (!methodIDgetExternalStorageDirectory) goto bailAndroid;
    jobject objectFile = (*env)->CallStaticObjectMethod(env, classEnvironment, methodIDgetExternalStorageDirectory);
    exception = (*env)->ExceptionOccurred(env);
    if (exception) {
        (*env)->ExceptionDescribe(env);
        (*env)->ExceptionClear(env);
    }
    
    // Call method on File object to retrieve String object.
    jclass classFile = (*env)->GetObjectClass(env, objectFile);
    if (!classFile) goto bailAndroid;
    jmethodID methodIDgetAbsolutePath = (*env)->GetMethodID(env, classFile, "getAbsolutePath", "()Ljava/lang/String;");
    if (!methodIDgetAbsolutePath) goto bailAndroid;
    jstring stringPath = (*env)->CallObjectMethod(env, objectFile, methodIDgetAbsolutePath);
    exception = (*env)->ExceptionOccurred(env);
    if (exception) {
        (*env)->ExceptionDescribe(env);
        (*env)->ExceptionClear(env);
    }
    // Extract a C string from the String object, and chdir() to it.
    const char *wpath3 = (*env)->GetStringUTFChars(env, stringPath, NULL);
    if (chdir(wpath3) != 0) {
        fprintf(stderr, "Error: Unable to change working directory to %s.\n", wpath3);
        perror(NULL);
    } else if (path) {
        if (chdir(path) != 0) {
            fprintf(stderr, "Error: Unable to change working directory to %s.\n", path);
            perror(NULL);
        }
    }
    
    (*env)->ReleaseStringUTFChars(env, stringPath, wpath3);
    
    goto retAndroid;
    
bailAndroid:
    fprintf(stderr, "Error: JNI call failure.\n");
    ret = -1;
retAndroid:
    if (isAttached) (*gJavaVM)->DetachCurrentThread(gJavaVM); // Clean up.
    return (ret);
}

关于android - 应用程序在 native 代码中的外部存储路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19568354/

相关文章:

android - 将构建类型 ("debug"/"release") 传递给 ndk-build

android - 如何调用Google Play星级评分对话框?

c++ - 一个类如何继承自己?

c++ - 带 INCLUDEPATH 的 qmake 忽略依赖项

c++ - C++中的字符串反转不会用新字符串替换旧字符串

android - 如何在Android Studio中使用ndk-build的自动调用

Android textview over imageview 背景透明

android - 每个 ListView 行中的按钮按下状态问题

android - 在 Android 上保持单一数据库连接

android - 如何设置eclipse在构建过程中不删除某些文件