1

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??

xnok
  • 299
  • 2
  • 6
  • 35
  • 1
    You could write the file to your apps private storage and when it's done writing use the SAF to let the user export it to some location of their choosing. – Tyler V Jul 20 '22 at 04:24
  • Is there any example how to do it? I honestly have little experience working with android storage was it was before, and I got even more confused with the mandatory use of SAF – xnok Jul 20 '22 at 04:45
  • Have a look [here](https://developer.android.com/training/data-storage). Use `getExternalFilesDir()` or `getExternalCacheDir()` – Tyler V Jul 20 '22 at 05:13
  • I updated my question with this attempt using getExternalFilesDir(). I was able to create a dir to my path and supposedely a text file, but when I open it on my Android to confirm it's creation, it's an empty directory – xnok Jul 20 '22 at 08:33
  • Did you check the logcat to see if `e.printStackTrace()` produced any output? – Tyler V Jul 20 '22 at 14:22
  • Also, check [the docs](https://developer.android.com/reference/android/content/Context#getExternalFilesDirs(java.lang.String)), it doesn't sound like you can pass your own type string to `getExternalFilesDir`? "May be null for the root of the files directory or one of the following constants..." – Tyler V Jul 20 '22 at 14:34
  • Found a working answer here: https://stackoverflow.com/questions/71378449/create-external-file-targeting-android-12. Only one that actually worked – xnok Jul 21 '22 at 07:45

1 Answers1

0

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();
    }
}
xnok
  • 299
  • 2
  • 6
  • 35