0

I'm still newbie. I'm making an android application to compose music. So the user can create their own music. How to save what user select in the interface? Example: there is 3 button to select, button 1 user select "a" button 2 user select "c" and button 3 user select "f". how to save it into file like .txt file and then load it when user want to play it again?

sorry if you don't really understand my language. maybe can you give me some some example save and load into file? i read some in stackoverflow but it's too many and I'm still not found my answer.

1
  • Google about SharedPreferences. That's the easiest way to save and persist data between application sessions.
    – NigelK
    Nov 29, 2013 at 21:19

2 Answers 2

0

you can use to save a file:

File newxmlfile1 = new File(Environment.getExternalStorageDirectory()+"/kadirGameLevels");
   newxmlfile1.mkdirs();


   int i = 1;
   File newxmlfile;
   do{
       String filename = i+".lvl";
       newxmlfile = new File(Environment.getExternalStorageDirectory()+"/kadirGameLevels/"+filename);
       i++;
   }while(newxmlfile.exists());

   try{
           newxmlfile.createNewFile();
   }catch(IOException e){
           Log.e("IOException", "exception in createNewFile() method");
   }
   //we have to bind the new file with a FileOutputStream
   FileOutputStream fileos = null;        
   try{
           fileos = new FileOutputStream(newxmlfile);
   }catch(FileNotFoundException e){
           Log.e("FileNotFoundException", "can't create FileOutputStream");
   }

and to get file to your app:

    File file = new File(Environment.getExternalStorageDirectory() + "/kadirGameLevels/1.lvl");
        DataInputStream stream = new DataInputStream(new FileInputStream(file));
1
  • still not really understand. example: String file1 = "a"; String file2 = "b"; String file3 = "c"; i want to save it into a file, lets use .txt maybe so inside the txt will be this 3 variable. and somehow i want to load it into my app. so the app will know that file1 = "a" file2 = "b" file3 = "c"
    – Scramble
    Nov 30, 2013 at 4:25
0

Why file? Why not shared preference ?

 private void saveCurrentScore(long currentScore) {
            try {
                android.content.SharedPreferences.Editor editor = prefs.edit();
                editor.putLong("currentscore", currentScore);
                editor.commit();

            } catch (Exception e) {
                e.printStackTrace();
            }

        }


        private String getCurrentScore() {
            try{prefs = PreferenceManager.getDefaultSharedPreferences(activity);
            return ""+  prefs.getLong("currentscore", 0);
            }catch(Exception e)
            {
                e.printStackTrace();
            }
            return "";
        }

NOTE:- If you are using andengine take a look at SecureSharedPreferences too

2
  • because i want the user can share their data among them. in this app i make an app to compose music. there's no score in my app. i want to save the data of my composed music in file like .txt then load it when needed into the app.
    – Scramble
    Dec 3, 2013 at 7:26
  • You can save the file in your storage and save the path to that file in shared preference Dec 3, 2013 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.