saveJSONObject ERROR!!

edited December 2016 in Android Mode

In the java mode the code work when I use Android mode it don't work anymore,it only crash the app how can I make it work that.

JSONObject data;

int score=2;

int record;

void setup() { data = loadJSONObject("data.json");}

void draw () {

 record = data.getInt("record");

record=score;

data.setInt("record", record);

saveJSONObject(data, "data/data.json");}

Answers

  • Are you trying to append data or to overwrite the data in your original file?

    Do you have enabled permissions to write on your phone? Check the manifest file, hit ctrl+k and open manifest.xml to check for what permissions you have so far.

    Kf

  • What permission do I have to search for?

  • edited December 2016

    This is my file and my AndroidManifest

  • In the Processing IDE in android mode, go to menu Android>>sketch permissions and check writing_external_storage. What this does is to add a couple of tags in your manifest file related to permissions.

    For more info check https://developer.android.com/guide/topics/security/permissions.html

    Kf

  • It doesn't change nothing it still crash and in theory i'm not accessing to any external storage

  • The app in theory should have access to his local file

  • Ok, that didn't work. My next step will be either:

    1. Use a lower frame rate. You are reading and saving in draw()
    2. Read and write in a control manner for testing purposes. For example, only read or write after a a signal is detected from the touchscreen or perform IO action every so many seconds.
    3. Try testing your code with loadString and saveString instead.

    This is not a final solution but more to understand what is happening to your program.

    Kf

  • edited December 2016

    Also here there is a problem with save string

    String lines[];
    
    int score=2,a=1;
    
    void setup() { lines = loadStrings("data.txt");}
    
    void draw () {
    
    if (a=1){
    
    int[] nums = int(split(lines[0]),' ')
    
    nums[0]=score;
    
    lines[0] = join(nf(nyms,0), "")
    
    saveStrings("data/data.txt",lines)
    
    a=0;
    }}
    
  • If I remove saveStrings the app don't crash

  • edited December 2016

    @MrAlko99=== i think that it crashes because you use a file separator in the filename, this works with java but not for android.

  • yeah but then how can I read the file from the main folder?

  • @MrAlko99===

    As for reading you can use the standard ways=

    String myString = json.getString("some string")...

    writing is more difficult because Android does not allow to modify the values stored in the ApplicationContext (assets or res) at runtime

    So if you want read/write you have

    1) to put your initial .json in your data folder

    2) to copy it by code on sdcard

    3) then you can access to it with fileIO and modify if needed

    this is easy with a text file but i never try with jSon; i ll give a look.

  • Yeah but I want that the code can modify the .json file

  • edited December 2016 Answer ✓

    @MrAlko99===

    i have tried and it works.

    • you put your initial json file in the assets or data folder
    • in setup() you create a method with fileIO to verify wether the json file is already in the SDcard or not; of course, first time it will not be
    • if not you copy your json file from assets (or data folder) to SDcard
    • if yes you load your json object from the sd card
    • at this moment you can setInt() or setString() as simply as usually
    • you can save when you want (mouseReleased or.....) using the path to your file in SD card.
    • and add write external storage permissions
  • Thank you very much when I will go back home I will fix it, how it would be the directory for a file in the external storage?

  • edited December 2016

    @MrAlko99=== depends; so you have to ask for that (in your setup, try!)

        String directory = new String(Environment.getExternalStorageDirectory()
            .getAbsolutePath());
        println(directory);
    

    of course you have to import::: import android.os.Environment;

  • edited December 2016

    @MrAlkoo99===

    using this way and json will work; yet it is perhaps too much if you want only to save 1 or 3 values (string, int, arrays) - as for a score in a game; in this very basic case i dont think that json is mandatory; as for me i would use SharedPreferences from android.

  • edited December 2016

    @MrAlko99===

    i have tried and it works.

    you put your initial json file in the assets or data folder in setup() you create a method with fileIO to verify wether the json file is already >in the SDcard or not; of course, first time it will not be if not you copy your json file from assets (or data folder) to SDcard if yes you load your json object from the sd card at this moment you can setInt() or setString() as simply as usually you can save when you want (mouseReleased or.....) using the path to your file >in SD card. and add write external storage permissions

    How you done that, and how do I import libraries??

    Initial not working code

    import android.os.Environment;
    
    String directory = new String( 
                                  Environment.getExternalStorageDirectory()
                                  .getAbsolutePath()                       );
    
    println(directory);
    

    P.S. This code actually work for the AbsolutePath

  • I believe you got the error when you tried to run it. Maybe try to put it in context. To do that, search for an existing example in the forum:

    https://forum.processing.org/two/search?Search=getExternalStorageDirectory

    After you find an example, configure it to your case and run it. The way you run your code should not work in Android mode (I think... I haven't checked it myself as I don't dig under the hood of the Android mode engine).

    Also, no screenshots of your problems. Copy and paste your code and your error messages. This will allow to: 1. Run your application by other forum goers and reproduce issues 2. It will make it legible (some screenshots are bad) 3. People can search for your error in this forum

    Kf

  • How can I check if there is a file in a directory

  • edited December 2016

    SOLVED

    JSONObject data,nullo;
    
    void setup(){
    nullo = loadJSONObject("nullo.json");
    fileExists(dataPath("/sdcard/snap_the_duck/data.json"));
    }
    
    boolean fileExists(String path) {
      File file=new File(path);
      boolean exists = file.exists();
      if (exists) {
        data=loadJSONObject("/sdcard/snap_the_duck/data.json");
        return true;
      }
      else {
        saveJSONObject(nullo, "/sdcard/snap_the_duck/data.json");
        return false;}
      }
    
    void draw(){ if(something happens){
      data.setInt("record", record);
      saveJSONObject(data, "/sdcard/snap_the_duck/data.json");
    } }
    

    Thank you very much for all your help @kfrajer @akenaton

Sign In or Register to comment.