title: Android: File IO from SD card using the NDK
body: <p>I am trying to write onto an output file using the NDK, and it doesn't seem to be working. The code I wrote looks like this:</p>
  
  <pre><code>JNIEXPORT jdouble JNICALL Java_com_example_sdcardtest2_CppMethods_nativeCalculations (JNIEnv* env, jclass clazz, jdouble dub){
      jdouble hub = dub + 10;
      FILE* file = fopen("/sdcard/textTest.txt","w+");
      fputs("HELLO WORLD!\n", file);
      fclose(file);
  return hub;
  }
  </code></pre>
  
  <p>The reason why the return type is a double, is that I am planning on using the function for another purpose, but I'm trying out file io first. In the logcat, I am getting the following error: </p>
  
  <pre><code>Fatal signal 11: (SIGSEGV) at 0x00000000c (code = 1) .....
  </code></pre>
  
  <p>Funny thing is, when I try to write to or read from the same file in the caller activity using java file io, I am not having any problems. And I made sure that I have write permissions in the manifest file. Also, if I remove the <code>FILE* file ...</code> line, and try to use the returned double value from the function, the program runs fine. Can someone please help? Could the problem be something I should include in the Android.mk or Application.mk files, for example?</p>
  
  <p><strong>UPDATE</strong>
  I'm using a Samsung galaxy note 2, if that makes any difference</p>
  
  <p><strong>UPDATE2</strong></p>
  
  <p>Here's the full code:</p>
  
  <p>java activity:</p>
  
  <pre><code>public class MainActivity extends Activity {
  private TextView text1;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      this.text1 = (TextView) findViewById(R.id.textView1);
      double d = CppMethods.nativeCalculations(2.80);
      String s = "The value of d is = " + d;
  
      File f1 = new File("/sdcard/textTest.txt");
      InputStream is = null;
      try {
           is = new FileInputStream(f1);
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      }
  
      Scanner reader = new Scanner(is);
  
  
      this.text1.setText(reader.nextLine().subSequence(0, s.length()));
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
  }
  
  }
  </code></pre>
  
  <p>Here's the native method:</p>
  
  <pre><code>/* DO NOT EDIT THIS FILE - it is machine generated */
  #include "com_example_sdcardtest2_CppMethods.h"
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  
  /*
   * Class:     com_example_sdcardtest2_CppMethods
   * Method:    nativeCalculations
   * Signature: (D)D
   */
  
  JNIEXPORT jdouble JNICALL Java_com_example_sdcardtest2_CppMethods_nativeCalculations
    (JNIEnv* env, jclass clazz, jdouble dub){
      jdouble hub = dub + 10;
      FILE* file = fopen("/sdcard/textTest.txt","w+");
      fputs("hello, world\n", file);
      fclose(file);
  
  
  
  
      return hub;
  }
  </code></pre>
  
  <p>Here's Android.mk:</p>
  
  <pre><code>LOCAL_PATH := $(call my-dir)
  include $(CLEAR_VARS)
  LOCAL_SRC_FILES := com_example_sdcardtest2_CppMethods.c
  LOCAL_MODULE := com_example_sdcardtest2_CppMethods
  include $(BUILD_SHARED_LIBRARY)
  </code></pre>
  
  <p>And here's the manifest:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.sdcardtest2"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="17" /&gt;
  
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"&gt;&lt;/uses-permission&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.sdcardtest2.MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Last update: I tried the same code on someone else's phone, also a galaxy note 2, and it worked. It seems as though the problem was with my phone. <em>sigh</em></p>