0

I am new to java and android development and i am struggling with an app i am trying to write.

Basically the app is for the hockey team i coach and i am trying to record stats from the game.

I have the app running as i want except i want to be able to save the stats at the end of the game to a file on the sd card so i can be accessed later on to review and be reset so a new game can be recorded.

Currently i have a new activity opening when a save button is pressed which contains an edittext field so i can name the file what i want, but i cannot work out how to get the information from the activity where the information is being recorded.

So far i have a class called Savetosd which is loaded which contains the following code:

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Savetosd extends Activity implements OnClickListener {

Button buttonsave;
EditText filename;
boolean isSDAvail = false, isSDWriteable = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);
    buttonsave = (Button) findViewById(R.id.buttonsave);
    filename = (EditText) findViewById(R.id.filename);
    buttonsave.setOnClickListener(this);
    checkSDCard();
}

private void checkSDCard() {
    // TODO Auto-generated method stub
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // write
        isSDAvail = true;
        isSDWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // read only
        isSDAvail = true;
        isSDWriteable = false;
    } else {
        // stink
        isSDAvail = false;
        isSDWriteable = false;
    }
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.buttonsave:
        Log.d("Hockey", "Games");
        if (isSDAvail && isSDWriteable) {
            File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            String name = filename.getText().toString();
            File file = new File(path, name + ".txt");

            try {
                path.mkdirs();
                FileOutputStream fOut = new FileOutputStream(file);
                OutputStreamWriter myOutWriter = 
                                        new OutputStreamWriter(fOut);
                myOutWriter.write(R.layout.activity_main);
                myOutWriter.close();
                fOut.close();
                Toast.makeText(getBaseContext(),
                        "Done writing SD " +filename.getText(),
                        Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Toast.makeText(getBaseContext(), e.getMessage(),
                        Toast.LENGTH_SHORT).show();
            }
            finish();
        }
        break;
    }
}
}

A file is being created but there is nothing in it as i guess i am not telling it which textviews and their values to grab, but i can't work out how to do this. Also the file is being created in the downloads directory in internal memory as it is being told to but i want it to be saved on the sdcard how do i do that?

  • these might help: http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder and http://stackoverflow.com/questions/4531396/get-value-of-a-edit-text-field goodluck – Ryan S Jul 06 '13 at 01:58

1 Answers1

0

Why are you calling this?

  myOutWriter.write(R.layout.activity_main);

Also, if you do not want to write your file to the Downloads directory then simply change the path from

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

to

 String path = Environment.getExternalStorageDirectory() + "/HockeyGameData.txt"
 File file = new File(path);

Or whatever you want to name your file. It makes sense that the file is getting created, but I can't see where you're writing the results of your TextView to it. If you're getting your TextView data from another Activity, you will need to pass that String into this Activity, which is responsible for writing to the external storage.

Of course, you could just reformat your code altogether and do all that logic from within a single Activity. I don't see why 2 Activities are necessary for what you're trying to accomplish.

Jade Byfield
  • 4,668
  • 5
  • 30
  • 41