Cannot open file through JNI

asked 2015-10-26 10:16:42 -0500

alexiosk gravatar image

updated 2015-10-26 10:17:29 -0500

I have copied a txt file in my device's sdcard. In the Java side of my Android app (i.e. MainActivity.java) I can open the file through this code:

    StringBuilder text = new StringBuilder();
    try {
        File sdcard = Environment.getExternalStorageDirectory();
        File file = new File(sdcard,"/android-opencv/file.txt");

        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
            text.append(line);
            text.append('\n');
        }
    }
    catch (IOException e) {
        e.printStackTrace();

    }

In my cpp file I try to open the same file doing the following:

  FILE* file = fopen("/storage/emulated/0/android-opencv/file.txt","r+");
  if (file != NULL)
        return env->NewStringUTF("Opened file");
  else
        return env->NewStringUTF("Error!");

I have added reading permissions in the AndroidManifest.xml:

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

and I have checked the file path of the external storage directory that it is "/storage/emulated/0/"

Any ideas why this might be happening?

edit retag flag offensive close merge delete