0

I have the following code. I am trying to update my app from API 30 (Android 11) to 32 (Android 12L), and for some strange reason, in this code file.exists always returns false.

public void getBiSymControllerProfile() {
    BufferedReader br = null;
    FileReader fr = null;
    File folder;
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.Q ||Environment.isExternalStorageLegacy()) {
        folder = new File(Environment.getExternalStorageDirectory()
                + "/bisym");
        if (!folder.exists())
            folder.mkdir();
    } else {
        folder = getExternalFilesDir(null);
    }
    File f = new File(folder.toString() + "/.bisymController");
    if (f.exists()) { //always returns false in Android 12L
        controllerFileExists = true;
        Log.d("Log", "File exists");
        try {
            fr = new FileReader(f);
            br = new BufferedReader(fr);

            String temp1;
            while ((temp1 = br.readLine()) != null) {
                temp1 = temp1.trim();
                String[] splitString = temp1.split("\\s+");
                readDeviceNames.add(splitString[0]);
                expDates.add(splitString[1]);

                Log.d("Log", "Device: " + splitString[0]);
                Log.d("Log", "Exp: " + splitString[1]);
            }
        } catch (IOException e) {

            Log.e("BiSym", "Could not read file!");

        } finally {
            try {
                if (br != null)
                    br.close();
                if (fr != null)
                    fr.close();
            } catch (IOException ex) {
                Log.e("BiSym", "Could not close file!");
            }
        }
    } else {
        Log.d("Log", "File does not exist");
        readController = false;
        controllerFileExists = false;
    }
}

Is there any reason why this would always return false?

Pink Jazz
  • 784
  • 4
  • 13
  • 34
  • `if (!folder.exists()) folder.mkdir();` If you want to read a file than it makes no sense to create a directory if the directory does not exist as then certainly the file will not exist too. Moreover the code for creating a directory should be: `if (!folder.exists()) if(!folder.mkdir();) return;`. – blackapps Mar 17 '23 at 16:51

0 Answers0