Reputation: 10755
I am writing application for Android and use native code, And test it on Android-Emulator. In order to see what happens in JNI code I create file in Android /data/LogTest/ folder and write my log information in it.
FILE * pFile;
pFile = fopen ("/data/LogTest/Log.txt"", "w");
// ....
// Write some logs to file ...
// ....
When I run in the first time Android application everything goes Okay, I can see logs in Log.txt file. But when I close Android application and run it again nothing happening. Like application can't write Logs to the file second time.
I think that the main reason of this problem is that, when I create file at first time the creator application Name is for ex. 456 after when I try to write some more information to file application Name is for ex. 856 and so application 856 can't write to file that have created application 456.
Upvotes: 4
Views: 3268
Reputation: 4838
your code is generating an error on my emulator. Yet you say
When I run in the first time Android application everything goes Okay, I can see logs in Log.txt file
Perhaps you could send us more of your code, so we can reproduce the error.
This is my attempt at reproducing 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 may be permissions. Describe it more for us.
PS:
Upvotes: 1
Reputation: 658
I think you are not allowed to write in that folder. Read through the answers here. Either use the sdk card or your application directory to store files.
File Operations in Android NDK
Upvotes: 0