*我从最近几天开始遇到将数据写入文件的问题。我正在从设计的相机应用程序中捕获图像。我正在尝试将单击图像的时间戳值写入 csv 文件。我能够正确生成 .csv 和图像文件并将其存储到 sd 卡上,但是在将时间戳数据写入 csv 文件时,我注意到 .csv 文件是空的。有人可以帮我解决这个问题。我附上代码位以供参考。谢谢你。
public class CameraActivity extends Activity {
private static final String LOG_TAG = "CameraActivity.java";
private static final int IMAGE_CAPTURE = 0;
File fileName;
PrintWriter captureFile;
double timeStamp = 0;
int x = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
try {
// intent to start device camera
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// you can also create your own filename or path
String fileN = "camera_app";
//String fileName = String.valueOf(System.currentTimeMillis());
// where do you want to save the images
String path = Environment.getExternalStorageDirectory() + "/"
+ fileN + ".jpg";
File file = new File(path);
// if the file name already exists, append __x on the file name
do {
path = Environment.getExternalStorageDirectory() + "/"
+ fileN + "__" + x + ".jpg";
file = new File(path);
captureFile = null;
fileName = null;
sdcard();
x++;
} while (file.exists());
Uri outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
// 0, this code will be returned in onActivityResult() when the
// activity exits.
startActivityForResult(intent, 0);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
private void sdcard() {
try {
fileName = new FileEnvironment.getExternalStorageDirectory(),"CameraData"+ "__" + Integer.toString(x) +".csv" );
FileWriter writer = new FileWriter(fileName,false);
captureFile = new PrintWriter (writer);
} catch (Exception e) {
e.printStackTrace();
}
}
// activity exits
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
if (requestCode == IMAGE_CAPTURE) {
if (resultCode == RESULT_OK) {
if( captureFile != null ){
saveTimeStamp();
}
takeAnother();
}
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void saveTimeStamp() {
try {
timeStamp=System.currentTimeMillis();
captureFile.println(timeStamp + "," + x);
Log.d(LOG_TAG, "Saved TimeStamp.");
captureFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onBackPressed() {
if( captureFile != null ) {
try {
captureFile.close();
// writer.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
CameraActivity.this.finish();
}
public void takeAnother() {
try {
new AlertDialog.Builder(this).setTitle("The Code Of A Ninja")
.setMessage("Do you want to take another picture?")
.setPositiveButton("YES", new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
arg0.dismiss();
Intent nextActivity = new Intent(
CameraActivity.this, CameraActivity.class);
CameraActivity.this.finish();
startActivity(nextActivity);
}
})
.setNegativeButton("NO", new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
arg0.dismiss();
Toast.makeText(CameraActivity.this,
"Done taking picture.", Toast.LENGTH_LONG)
.show();
// go to main activity
CameraActivity.this.finish();
}
}).show();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}*