Unable to write file to Android emulator.

Description of the problem

I am writing an Android application and using my own code, and testing it on an Android emulator . To find out what is going on in the JNI code, I create a file in the Android / data / LogTest / folder and write my log data to it.

FILE * pFile; pFile = fopen ("/data/LogTest/Log.txt"", "w"); // .... // Write some logs to file ... // .... 

When I run the Android application for the first time, everything is going well, I can see the logs in the Log.txt file. But when I close the Android application and start it again, nothing happens. How an application cannot write logs to a file a second time.

Do-it-yourself ideas

I think the main reason for this problem is that when I create the file for the first time, the name of the creator application is for ex. 456 after I try to write additional information to a file. 856 and therefore, application 856 cannot write to the file that created application 456 .

Question

  • How can I run an application with the same name so that Android allows me to write to a file a second time.
  • Or maybe the main reason - the problem is not that the application gets rundom names every time.
+4
source share
2 answers

I think you are not allowed to write in this folder. Read the answers here. Use the sdk card or application directory to store files.

File Operations in Android NDK

0
source

your code generates an error on my emulator. But you say

When I run the Android application for the first time everything is going well, I can see the logs in the Log.txt file

Perhaps you could send us more of your code so that we can reproduce the error.
This is my attempt to reproduce your question.

 #include<stdio.h> #include<jni.h> #include<android/log.h>//allow android logging #include<errno.h>//for errors #include<string.h> #define LOG_TAG "DEO MSG"//all my logs are labeled with this #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) void Java_com_deo_MyActivity_writeLogFileUsingC(JNIEnv * env, jobject thisObject) { char filename[]="/data/LogTest/Log.txt"; LOGE("native method started");//is used exactly like the usual printf() FILE * pFile; LOGE("trying to open file for writing"); pFile= fopen(filename, "w"); if(pFile==NULL) { LOGE("Failed to open the file %s in mode 'w'.(DETAILS)%s ",filename,strerror(errno)); } else { LOGE("trying to write to file"); fprintf(pFile,"logExample "); //example of a log. fclose(pFile);//safely close our file LOGE("file writing done"); } } 

The error it generates in logcat is

 ERROR/DEO MSG(816): Failed to open the file /data/LogTest/Log.txt in mode 'w'.(DETAILS)No such file or directory 

I think my problem with your code might be a resolution. Describe this more for us.
PS:

  • I personally prefer using logcat for debugging compared to log files
+1
source

All Articles