简体   繁体   中英

Can't write to the file in android emulator

Problem Description

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.

Self Ideas

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 .

Question

  1. How I can launch application with same name, in order Android let me to write to file in second time.
  2. Or maybe the main reason o problem is not that Application every time get rundom names.

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:

  • i personally prefer using the logcat for debugging compared to log files

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM