Pergunta

I'm calling C code by JNI in android app.

Every thing is OK in calling, but when I put this code:

FILE* fp = fopen("/storage/sdcard0/input.txt","w+");
if(fp==NULL)
    return(*env)->NewStringUTF(env,"n");
else
    return(*env)->NewStringUTF(env,"y");

it gets "n". I'm sure that the path is true and I tried this command on my mobile's terminal:

cat /storage/sdcard0/input.txt

and it got me the file's contents.

Edit:

I tried to change the permission of the file by this code but it gave me the same response:

void changePerm()
{
    Process chperm;
    try {
        chperm=Runtime.getRuntime().exec("su");


   DataOutputStream os = 
          new DataOutputStream(chperm.getOutputStream());
        os.writeBytes("chmod 777 /storage/sdcard0/input.txt\n");
        os.flush();

        os.writeBytes("exit\n");
        os.flush();

          chperm.waitFor();

} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}

I also add this permission and no thing changed:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Foi útil?

Solução

I take it that the permissions on the file are in fact 0777.

You added android.permission.READ_EXTERNAL_STORAGE, but you try to open for writing

FILE* fp = fopen("/storage/sdcard0/input.txt","w+");

You can get the error by checking for errno and get a message with strerror.

You must either open the file for reading only "r" or add permission for writing WRITE_EXTERNAL_STORAGE

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top