.txt files in Android Mode

edited July 21 in Android Mode

I'm upgrading an android game i've built some months ago ([https://play.google.com/store/apps/details?id=com.Sarothlon.MazeGenerator&hl=it](See it here)) so I first tested the new version on java mode and than in android mode where I'm not able to write textFiles i used to save some informations about the scores and some settings. The problem is that in android mode the PrintWriter class doesn't work. I think this is caused by the path of the argument of createWriter("data/textfile.txt"); I red that in android mode the path of the file should be absolute and not relative, but i couldn't find the textfile.txt searching in my smart phone. In addiction if an absolute path is requested it's still a problem because I think the path can change according to the different users'preferences (for example sd card or internal storage).

How can i fix this problem?

Answers

  • For example this doesn't work:

    `//the file example.txt is saved in the data directory of the sketch //and in the file there is only the string "Hello World"

    PrintWriter output;

    void setup(){ fullScreen();

     String data[] = loadStrings("example.txt");
     println(data[0]);
    
     output = createWriter("data/example.txt");
     output.println("123456789");
    
     String changedData[] = loadStrings("example.txt");
     println(changedData[0]);
    

    }`

    In java mode the output would be Hello World 123456789

    But in android mode the output is Hello World Hello World

  • @kfrajer I'm trying this https://developer.android.com/guide/topics/data/data-storage.html#filesInternal with this sketch:

    import java.io.FileOutputStream;
    import android.content.Context;
    void setup(){
      size(330, 550);
     String FILENAME = "gazzo.txt";
    String string = "saro";
    
    FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    fos.write(string.getBytes());
    fos.close();  
    }
    

    but processing doesn't recognise the openFileOutput() function and gives me this error

    The function "openFileOutput(String, int)" does not exist

  • https://stackoverflow.com/questions/3625837/android-what-is-wrong-with-openfileoutput

    So you can check this post: https://forum.processing.org/two/discussion/comment/100539/#Comment_100539

      act = this.getActivity();
      context = act.getApplicationContext();
    

    and then context.openFileOutput(...) Notice this is untested.

    Kf

  • @kfrajer it still doesn't work

    Immagine

  • I am not familiar with openFileOutput() and I can only advise based on my experience as documented in previous posts. I am not able to test your code as I do not have an Android environment available but until later in the day. Keep in mind Android Mode is having a revamp and some examples that ran in previous mode (Android Mode 3.0.2 and before) might not work with the latest Android mode (Version 4.0 beta ?). To make them work, they might need some minor tweaking. I will try later and I let you know. Just for curiosity, did you try my first link where they used other functions to do what you want to do?

    By the way, can you comment if you have set the proper sketch permissions for writing to external storage? This is not related to your error btw...

    Kf

  • @kfrajer I've found out a way to solve this problem.

    1) Sketch permissions : •READ_EXTERNAL_STORAGE •WRITE_EXTERNAL_STORAGE

    2)The files you wonna rewrite MUST NOT be in the data folder of the sketch, they MUST NOT exist when you load the program to the smartphone, or the function I will show you later won't have any effect on the files(I don't know why).The strange thing is that if you call my function, a new file will be created in the data folder and it will be accessible from the loadString() function.But if you create the file manually, before to load the program, loadString() function will be able to read only the original file, not any modification applied to it by my function.

    3)Imports:

    import java.io.FileOutputStream;
    import android.content.Context;
    import android.app.Activity;
    

    4)I wrote a function to write any file

    void writeFile(String filename, String string){
         Activity act;
         Context context;
         FileOutputStream outputStream;
         act = this.getActivity();
         context = act.getApplicationContext();   
          try {
              outputStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
              outputStream.write(string.getBytes());
              outputStream.close();
          }
          catch (Exception e) {
              println(e);
          }     
       }
    

    For more detailed informations see this page https://developer.android.com/training/basics/data-storage/files.html#GetWritePermission

  • edited July 21

    @sarrio===

    • Using openFileOutput() creates your file in the internal storage files folder.
    • If the file exists it writes (not add or append) your new string.
    • i dont think that the file is created inside the data folder; files/ is internal to android context for your app and you cannot "see" it.
    • if you want to be sure add some code like this one:

      File myFile;
      Activity act;
      
          // --------setUp:
      act = this.getActivity();
      myFile = act.getFilesDir();
      println (myfile)//// it return the path where the file is created (internal storage);
      //then you can use your openFileOutput() for writing or rewriting (if it already exists)
      
    • So it is absolutely normal that your method has no effects upon the file manually created in your data folder, even if they have the same name...

  • Great you got it working.

    For the file access, The PRIVATE parameter could be the culprit. Check this post: https://stackoverflow.com/questions/3625837/android-what-is-wrong-with-openfileoutput

    Also consider this: https://developer.android.com/reference/android/content/Context.html

    MODE_PRIVATE File creation mode: the default mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID).

    This further to consider from the previous link:

    MODE_WORLD_READABLE This constant was deprecated in API level 17. Creating world-readable files is very dangerous, and likely to cause security holes in applications. It is strongly discouraged; instead, applications should use more formal mechanism for interactions such as ContentProvider, BroadcastReceiver, and Service. There are no guarantees that this access mode will remain on a file, such as when it goes through a backup and restore.

    Kf

Sign In or Register to comment.