Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';t在android emulator中写入文件 问题描述_Java_Android_C_Android Emulator_Java Native Interface - Fatal编程技术网

Java Can';t在android emulator中写入文件 问题描述

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 ... // .... 当我第一

我正在为Android编写应用程序,使用本机代码,并在Android Emulator上进行测试。为了查看JNI代码中发生了什么,我在Android/data/LogTest/文件夹中创建了一个文件,并将日志信息写入其中

FILE * pFile;
pFile = fopen ("/data/LogTest/Log.txt"", "w");
// .... 
// Write some logs to file ...
// ....
当我第一次运行Android应用程序时,一切正常,我可以在Log.txt文件中看到日志。但当我关闭Android应用程序并再次运行时,什么也没发生。就像应用程序不能再次将日志写入文件一样

自我观念 我认为这个问题的主要原因是,当我第一次创建文件时,创建者应用程序名是ex.456,当我尝试向文件写入更多信息时,应用程序名是ex.856,因此应用程序856无法写入已创建应用程序456的文件

问题:
  • 如何启动同名应用程序,以便Android允许我在第二次写入文件。
  • 或者,问题的主要原因可能不是应用程序每次都获取rundom名称。

  • 我想你不允许在那个文件夹里写字。在这里通读答案。使用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进行调试

    只需附带说明,您还可以通过liblog向logcat发送日志消息。您必须将log.h文件和liblog包含到make文件中,然后才能在代码中使用u android_log_write()。这里有一篇详细的博客文章@谢谢你的支持,你的回答对我很有用,我从你的评论中学到了新的东西,但这不是我真正想要的,我需要写日志。txt检查如果
    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