Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android_app->activity->internalDataPath still NULL in 2.3.8 NDK r8

To give spec on where i tested this, HTC Desire S, Android 2.3.5 and ndk-r8.

I am having issues in ndk-r7b and in ndk-r8 accessing the local read write directories using android_app->activity->internalDataPath or externalDataPath as they are both NULL. I see that this has been posted in previous versions and an update was going to fix this according to this post:-

How do I write to the internal storage file system with NativeActivity?

Now perhaps this gets fixed in Ice Cream Sandwich but that's far from ideal as my tool chain supports backwards compatibility perfectly to catch those who don't update. So my question is is there a known fix or do i have to manually add the directory structure like "/data/data/com.example.mytest/files/somefile.dat" untill its fixed ?

like image 975
Dev2rights Avatar asked May 21 '12 09:05

Dev2rights


People also ask

What is SDK and NDK?

Android provides Native Development Kit (NDK) to support native development in C/C++, besides the Android Software Development Kit (Android SDK) which supports Java. [TODO] more.

What is NDK in android studio?

The Native Development Kit (NDK) is a set of tools that allows you to use C and C++ code with Android, and provides platform libraries you can use to manage native activities and access physical device components, such as sensors and touch input.

What is Native activity application?

The native-activity sample resides under the NDK samples root, in folder native-activity . It is a very simple example of a purely native application, with no Java source code. In the absence of any Java source, the Java compiler still creates an executable stub for the virtual machine to run.


1 Answers

The following works from NDK without use of Java:

const char* path = app->activity->internalDataPath;
if (!path) {
    JNIEnv* jni;
    app->activity->vm->AttachCurrentThread(&jni, NULL);

    jclass activityClass = jni->GetObjectClass(app->activity->clazz);
    jmethodID getFilesDir = jni->GetMethodID(activityClass, "getFilesDir", "()Ljava/io/File;");
    jobject fileObject = jni->CallObjectMethod(app->activity->clazz, getFilesDir);
    jclass fileClass = jni->GetObjectClass(fileObject);
    jmethodID getAbsolutePath = jni->GetMethodID(fileClass, "getAbsolutePath", "()Ljava/lang/String;");
    jobject pathObject = jni->CallObjectMethod(fileObject, getAbsolutePath);
    path = jni->GetStringUTFChars((jstring)pathObject, NULL);

    jni->DeleteLocalRef(pathObject);
    jni->DeleteLocalRef(fileClass);
    jni->DeleteLocalRef(fileObject);
    jni->DeleteLocalRef(activityClass);

    app->activity->vm->DetachCurrentThread();
}
like image 135
Mike Weir Avatar answered Sep 20 '22 12:09

Mike Weir