Java Can';t在android emulator中写入文件 问题描述
我正在为Android编写应用程序,使用本机代码,并在Android Emulator上进行测试。为了查看JNI代码中发生了什么,我在Android/data/LogTest/文件夹中创建了一个文件,并将日志信息写入其中Java Can';t在android emulator中写入文件 问题描述,java,android,c,android-emulator,java-native-interface,Java,Android,C,Android Emulator,Java Native Interface,我正在为Android编写应用程序,使用本机代码,并在Android Emulator上进行测试。为了查看JNI代码中发生了什么,我在Android/data/LogTest/文件夹中创建了一个文件,并将日志信息写入其中 FILE * pFile; pFile = fopen ("/data/LogTest/Log.txt"", "w"); // .... // Write some logs to file ... // .... 当我第一
FILE * pFile;
pFile = fopen ("/data/LogTest/Log.txt"", "w");
// ....
// Write some logs to file ...
// ....
当我第一次运行Android应用程序时,一切正常,我可以在Log.txt文件中看到日志。但当我关闭Android应用程序并再次运行时,什么也没发生。就像应用程序不能再次将日志写入文件一样
自我观念
我认为这个问题的主要原因是,当我第一次创建文件时,创建者应用程序名是ex.456,当我尝试向文件写入更多信息时,应用程序名是ex.856,因此应用程序856无法写入已创建应用程序456的文件
问题:
我想你不允许在那个文件夹里写字。在这里通读答案。使用sdk卡或应用程序目录存储文件
您的代码在我的模拟器上生成错误。然而你说 当我第一次跑步的时候 应用程序一切正常,我 可以在Log.txt文件中查看日志 也许你可以给我们发送更多的代码,这样我们就可以重现错误
这是我试图重复你的问题
#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");
}
}
我想我对你的代码的问题可能是权限。请为我们详细描述一下。PS:
- 与日志文件相比,我个人更喜欢使用logcat进行调试
fopen
失败:if(pFile==NULL)perror(“fopen”)代码>我喜欢这个链接。androidndk中的文件操作
ERROR/DEO MSG(816): Failed to open the file /data/LogTest/Log.txt in mode 'w'.(DETAILS)No such file or directory