无法将数据写入android中保存的文件

时间:2013-09-30 16:21:57

标签: java android

*由于将数据写入文件的问题,我最近几天陷入困境。我正在从设计的相机应用程序中捕获图像。我正在尝试写入将图像单击到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();
    }
}*

2 个答案:

答案 0 :(得分:0)

更改此行:

 FileWriter writer = new FileWriter(fileName,false);

到这一个:

 FileWriter writer = new FileWriter(fileName,true);

答案 1 :(得分:0)

我不熟悉您将数据写入文件的方法,但我首先要确保您在清单文件中具有适当的权限。以下XML应该是清单元素的子元素:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

这使Android可以写入内部和外部存储驱动器。