-1

hello all the tutorial on android external data storage are driveing me nuts

i have a OnOptionsCreatemenu that has 2 buttons "save" and "load" save works fine but load does not here is my code it wont let me iv commented down in the code also how do i make it let me save it in my own directory?...iv read every single tutorial on saving to ext storage like 5 times.....im just to dumb to get it! (please no links) then please make it into a new tutorial because it will be the most simplest breakdown of android ext data storage that even retarded chimpanzes like myself would understand

this is all meant to be one chunk of code......not sure why the code is showing in 2 codeboxes???

p.s sorry its my first time.....be easy ;)

private void save() {

    String saveLine = ((EditText)
            findViewById(R.id.captain)).getText().toString().trim();

    File sdcard = Environment.getExternalStorageDirectory();

    File file = new File(sdcard,"/myfile.txt");

    try {

        FileWriter fw = new FileWriter(file);

        BufferedWriter bw = new BufferedWriter(fw);

        bw.write(saveLine);

        bw.close();
        Toast.makeText(getApplicationContext(),"File saved!",Toast.LENGTH_LONG).show();
    } catch (IOException e) {

        Toast.makeText(getApplicationContext(),"Error writing file!",Toast.LENGTH_LONG).show();

    }
    {

//here it says i need to put a variable? why wont it let me use it like the save() function????

    private void load() {

      //  ensure();

        sdcard = Environment.getExternalStorageDirectory();

        File file1 = new File(sdcard,"/myfile.txt");


        try {

            BufferedReader br = new BufferedReader(new FileReader(file1));

           String loadline;

            if ((loadline = br.readLine()) != null) {

                String content=loadline.toString();

                ((EditText)
                        findViewById(R.id.captain)).setText(content);



            }

            br.close();

        }

        catch (IOException e) {

            Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();

        }

    }
  • Just for future knowledge, do not put please help and similar things in your post as they are frowned upon. Other than that welcome to SO. – ObieMD5 Jul 21 '13 at 02:39

1 Answers1

0

You seems to have problem with reading the file, right? For reading a file from SD card and setting it into EditText here is the code:

private void load(){

    File sdcard = Environment.getExternalStorageDirectory();

    //Get the text file
    File file = new File(sdcard,"myfile.txt");

    //Read text from file
    StringBuilder text = new StringBuilder();

    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;

        while ((line = br.readLine()) != null) {
            text.append(line);
            text.append('\n');
        }
    }
    catch (IOException e) {
        //You'll need to add proper error handling here
        Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
    }

    //Find the view by its id
    EditText ed = (EditText)findViewById(R.id.captain);

    //Set the text
    ed.setText(text);
}

Exact Source: How can I read a text file from the SD card in Android?

Hope this helps else comment.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • ok so im doing private void load() {.....then your code then it throws back "expression expected" between the load and the ()....thanks for your help in advance.. am i wrong for trying to use load as its own method? like save() – OnTHEvERGEOFaANURYSM Jul 21 '13 at 01:39
  • when i rebuild the project it says "java: illegal start of expression" then i can jump to source and it takes me to private void load() {....it still wants a expression???? im useing intelli j also in the OnOpyionsCreateMenu Case for load() says it cant resolve method?? – OnTHEvERGEOFaANURYSM Jul 21 '13 at 01:58
  • Are you facing the problem still? Please show that peice of code in the question for letting us help further. – Shobhit Puri Jul 21 '13 at 22:45
  • my code is the same peice of code thats posted it just throws the error i described in my last post – OnTHEvERGEOFaANURYSM Jul 22 '13 at 14:54
  • ok i got the method to work it wont load the actual file i saved but it throws the error "error loading file' so i know its working ...now i just got to figure out the error....logcat does not show any errors ...would it help if i used the previous File sdcard declaration? – OnTHEvERGEOFaANURYSM Jul 22 '13 at 17:16
  • Are you trying `File file1 = new File(sdcard,"/myfile.txt");` or `File file1 = new File(sdcard,"myfile.txt");`? check the name of the file once again. – Shobhit Puri Jul 22 '13 at 17:20
  • yeah i ran into that one too now im trying to do something eles see other post – OnTHEvERGEOFaANURYSM Jul 23 '13 at 14:43
  • Can you please point me to the other post that you are mentioning? – Shobhit Puri Jul 25 '13 at 22:26