3

While running the app on the emulator, system reports an error while cpp code trying to create a directory in folder "/home/cocadas/Workspace/android-project/JNIAppSample".

Java will call a JNI cpp function to create the directory.

The cpp source code is as following:

static int createEventDir(void)
{
  int    result;
  int    stringLen;
  time_t currentTime = time(0);
  struct tm * now = localtime(&currentTime);

  stringLen = sprintf(thisEventParms.eventDirectory,
                      ADAN_EVENT_BASE_DIR, now->tm_mon + 1,
                        now->tm_mday,        now->tm_hour,
                        now->tm_min,         now->tm_sec);

  if (stringLen > 0)
  {
    result = mkdir(thisEventParms.eventDirectory, 0700);
  }
  else
  {
    // TBD: Error, unable to make event directory
    result = -1;
  }
  return(result);
}

Android Studio debug reports ​result = -1 after executing result = mkdir(thisEventParms.eventDirectory, 0700);​ Also, debug reports thisEventParms.eventDirectory = "/home/cocadas/Workspace/android-project/JNIAppSample", which is expected.

After some research, I add one permission in manifest like the following:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

But it still gives result = -1

Any comment or suggestion?

Hong
  • 526
  • 6
  • 21
  • Possible duplicate of [App's path for external storage in native code](https://stackoverflow.com/q/19568354/608639) – jww Sep 16 '18 at 15:47

1 Answers1

3

I guess that the android emulator has the same structure as any android device.

So, /home/cocadas...etc doesn't exist in your emulator.

If the response is -1, usually seems that your path is not writable.

In conclusion, writable path could be "/sdcard/your_directory." and of course you will need to add the permission you written above.

If you want to get an writable path but inside of your apk use this code.

String config_path=m_context.getApplicationContext().getFilesDir().toString();

Cheers.

uelordi
  • 2,189
  • 3
  • 21
  • 36