我正确地将文件保存在SD卡中,但是不检索SD卡文本文件的数据并显示在另一个活动的微调器上。当我点击读取按钮时,数据显示在另一个活动的微调器上 当我点击微调器上的阅读按钮txt文件项目显示
mainactivity.java
btnWriteSDFile = (Button) findViewById(R.id.Write);
btnWriteSDFile.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Jour = display.getText().toString();
// write on SD card file data in the text box
try {
File myFile = new File("/sdcard/mysdfile.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(display.getText());
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(), "Done writing SD 'mysdfile.txt'", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
} // onClick
}); // btnWriteSDFile
btnReadSDFile = (Button) findViewById(R.id.Read);
btnReadSDFile.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// write on SD card file data in the text box
try {
File myFile = new File("/sdcard/mysdfile.txt");
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader( new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
}
display.setText(aBuffer);
myReader.close();
Toast.makeText(getBaseContext(), "Done reading SD 'mysdfile.txt'", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
list.java
Spinner sp1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
sp1 = (Spinner) findViewById(R.id.spinner1);
getFilesnames();
}
private void getFilesnames() {
String[] filenames=getApplicationContext().fileList();
List<String> list=new ArrayList<String>();
for(int i = 0; i < filenames.length; i++) {
list.add(filenames[i]);
}
ArrayAdapter<String> filenameAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
sp1.setAdapter(filenameAdapter);
}