android– 应用程序在本机代码中进行外部存储的路径

android– 应用程序在本机代码中进行外部存储的路径,第1张

概述我正在尝试为我的应用程序编写本机库,以便我可以在本机代码中执行所有文件 *** 作.我读到getExternalStorageDirectory()给出了目录外部存储路径.我的问题是如何在不将位置硬编码到某些字符串的情况下访问相同内容?androidndk中是否有任何函数可以提供与C代码中java的getExternalS

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

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

解决方法:

JNI是你的朋友,这不是太复杂,因为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:    ARLOGe("Error: JNI call failure.\n");    ret = -1;retAndroID:    if (isAttached) (*gJavaVM)->DetachCurrentThread(gJavaVM); // Clean up.    return (ret);}
总结

以上是内存溢出为你收集整理的android – 应用程序在本机代码中进行外部存储的路径全部内容,希望文章能够帮你解决android – 应用程序在本机代码中进行外部存储的路径所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1116064.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存