preferences in android

edited July 2016 in Android Mode

i m using image pixels to save the preferences of player in game. m saving the image in data folder with pref.save("data/preferences.png"); but when i deploy this to android device it show me error of not finding the data folder. when i save the image directly to the sketch folder it is not deployed with the code to the device. i write the code to create the image when nt available in skecth folder it creates the image in android but can't be able to change the pixel values of the image the code is given below

  if (loadImage("preferences.png")==null)
    {
      pref = createImage(6, 6, RGB);
      pref.loadPixels();
      for (int i = 0; i<36; i++)
      {
        pref.pixels[i] = color(1, 0, 0);
      }
      pref.updatePixels();
      pref.save("preferences.png");
      println("null");
    }
    else
    {
      pref=loadImage("preferences.png");
  //    println("preferences");
    }

Answers

  • Please, remember to use the combo box to select a categoy, Android in your cases. I moved your topics.

  • You coulda declared a variable to store the loaded image file once:
    PImage imgTmp = loadImage("preferences.png");

  • Answer ✓

    As far as I can tell, the data folder is merged into the assets folder in the APK file, which cannot be edited by the application (although this would be theoretically possible, it would be a breach of Google Play's policies). Instead, you should create a new file in the application's private directory - most Processing functions should work out of the box.

    Your approach to saving preferences - using individual pixels in an image file - is somewhat original, but I'm not sure how practical it is. I won't try to stop you, but I would highly recommend an alternative, such as a simple text file - or, even better, Android's SharedPreferences API which is designed specifically for this purpose.

  • saving preferences in the pixel works fine in the java mode or on desktop but create problem in android.

    can shared preferences directly be implemented in processing ide if yes kindly show me an example

    or if no the would i implement it in ecclipse by exporting the application

  • The SharedPreferences API should work with Processing. The link above contains information about how to use it - the only thing you'll need that it doesn't mention is the necessary import at the top of the sketch:

    import android.content.SharedPreferences;
    

    If you have any trouble, I can write a quick example (but please show what you have tried first!).

  • edited July 2014

    Thank u calsign :) work fine for me posting my code for others.

    SharedPreferences settings;
    boolean s ;
    void setup()
    {
      settings = getPreferences(MODE_PRIVATE);
    }
    
    void draw()
    {
      s = settings.getBoolean(sadam, false);
      if (s)
      {
        fill(0);
        rectMode(CENTER);
        rect(width/2, height/2, width/2, height/2);
      } else if (!s)
      {
        fill(255);
        rectMode(CENTER);
        rect(width/2, height/2, width/2, height/2);
      }
    }
    
    void mousePressed()
    {
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean(sadam, true);
      editor.commit();
    }
    
  • Im using this code but im getting the errors:

    The function getSharedPreferences(String, int) does not exist

    The function getPreferences(int) does not exist

        import android.content.SharedPreferences;
        import android.content.Context;
    
        void settings() 
        {
          SharedPreferences sharedPref = getSharedPreferences("Data", Context.MODE_PRIVATE);
          SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
          size(displayWidth, displayHeight);
        }
    
  • made it work with this code, dunno is it is the best way tho.

    import android.content.SharedPreferences;
    import android.preference.PreferenceManager;
    import android.content.Context;
    import android.app.Activity;
    
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    Activity act;
    
    void settings() 
    {
      act = this.getActivity();
      sharedPreferences = PreferenceManager.getDefaultSharedPreferences(act.getApplicationContext());
      editor = sharedPreferences.edit();
    }
    
  • @Asgardinho=== yes, that is the way because you are not in an activity but in a fragment

  • this is good stuffs!

Sign In or Register to comment.