我需要保存带有时间戳的条形码并将其保存在 .csv 文件中,然后通过蓝牙将其发送到另一台设备。我通过 SerialMagic Gear Keyboard 选项获取条形码,该选项将条形码作为输入
问题:当应用程序运行并且我输入数据时,当另一个设备接收到该文件时,该文件只包含最后一个条目。
我确信我在程序结构上犯了一些错误。如果是这样,请指出在哪里。
抱歉,代码很长。
package com.android.app;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Timestamp for file name e.g. Data_7-19-2012
SimpleDateFormat s1 = new SimpleDateFormat("MM-dd-yyyy_hh:mm:ss");
final String format1 = s1.format(new Date());
//Creating file name
final String FileName = "Data_"+format1+".csv";
final EditText addbarcode = (EditText) findViewById(R.id.editText1);
//getting file directory
final File dir = getFilesDir();
final File shareFile = new File(dir, FileName);
addbarcode.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
//case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
//to get Timestamp
SimpleDateFormat s = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
String format = s.format(new Date());
//get barcode in a variable
Editable barcode = addbarcode.getText();
final String DATASTRING = new String(""+barcode+","+format+"\n");
FileOutputStream fOut = null;
try {
fOut = openFileOutput(FileName , MODE_WORLD_READABLE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OutputStreamWriter osw = new OutputStreamWriter(fOut);
// Write the string to the file
try {
osw.write(DATASTRING);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
osw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
osw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* ensure that everything is
* really written out and close */
//For showing data recived on screen
FileInputStream fIn = null;
try {
fIn = openFileInput(FileName);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
InputStreamReader isr = new InputStreamReader(fIn);
//Prepare a char-Array that will
//* hold the chars we read back in.
char[] inputBuffer = new char[DATASTRING.length()];
// Fill the Buffer with data from the file
try {
isr.read(inputBuffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Transform the chars to a String
String readString = new String(inputBuffer);
TextView etData= (TextView)findViewById(R.id.textView3);
etData.setText(readString);
//Clear the editview for new entries
addbarcode.setText("");
return true;
default:
break;
}
}
return false;
}
});
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Uri u1 = null;
u1 = Uri.fromFile(shareFile);
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Person Details");
sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
sendIntent.setType("text/csv");
startActivity(sendIntent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}