简体   繁体   中英

How to write and constantly update a text file in Android

I have a camera that I am grabbing values pixel-wise and I'd like to write them to a text file. The newest updates for Android 12 requires me to use storage access framework, but the problem is that it isn't dynamic and I need to keep choosing files directory. So, this approach it succesfully creates my files but when writting to it, I need to specifically select the dir it'll save to, which isn't feasible to me, as the temperature is grabbed for every frame and every pixel. My temperature values are in the temperature1 array, I'd like to know how can I add consistently add the values of temperature1 to a text file?

EDIT: I tried doing the following to create a text file using getExternalFilesDir() :

  private String filename = "myFile.txt";
  private String filepath = "myFileDir";
    public void onClick(final View view) {
        switch (view.getId()){
            case R.id.camera_button:
                synchronized (mSync) {
                    if (isTemp) {
                        tempTureing();
                        fileContent = "Hello, I am a saved text inside a text file!";
                        if(!fileContent.equals("")){
                            File myExternalFile = new File(getExternalFilesDir(filepath), filename);
                            FileOutputStream fos = null;
                            try{
                                fos = new FileOutputStream(myExternalFile);
                                fos.write(fileContent.getBytes());
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            Log.e("TAG", "file: "+myExternalFile);
                        }
                        isTemp = false;
                        //Log.e(TAG, "isCorrect:" + mUVCCamera.isCorrect());
                    } else {
                        stopTemp();
                        isTemp = true;
                    }
                }
                break;

I can actually go all the way to the path /storage/emulated/0/Android/data/com.MyApp.app/files/myFileDir/ but strangely there is no such file as myFile.txt inside this directory, how come??

Working Solution:

public void WriteToFile(String fileName, String content){
    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
    File newDir = new File(path + "/" + fileName);
    try{
        if (!newDir.exists()) {
            newDir.mkdir();
        }
        FileOutputStream writer = new FileOutputStream(new File(path, filename));
        writer.write(content.getBytes());
        writer.close();
        Log.e("TAG", "Wrote to file: "+fileName);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM