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?