How to open and read a text file manually copied to download folder on Android 13

715 views Asked by At

I just updated to a new tablet running Android 13, and now my app can no longer open a text file that is manually copied to the /Download folder. Previously had been requesting READ and WRITE EXTERNAL STORAGE, but now even with those permissions now get IOException: open failed: EACCES (Permission denied). It has been a requirement for my app to open a specifically named text file (route.txt) that a customer copies to the /Download folder, and then copy the contents to a String var. Has Google now inhibited what has been allowed for over a decade? It will be a major nightmare to have to inform 20K+ users that their business logic must change :(

Have tried checking, and requesting READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE (if not granted). Once granted, then try to open the file (read to a String)

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

Even with permissions granted, I get the EACCES error when trying to open the file:

   static String readRouteFile(String sFile){
        Log.d(TAG, "readRouteFile(" + sFile + ")");

        File f = new File(sFile);

        if(f.exists()){
            Log.d(TAG, "readRouteFile.exists");

            StringBuilder sb = new StringBuilder();
            FileInputStream fis;
            String sbuf;
            try {
                fis = new FileInputStream(f);
                BufferedReader bReader = new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8));
                while ((sbuf = bReader.readLine()) != null ) {
                    sb.append(sbuf).append('\n');
                }

                bReader.close();
                fis.close();

                Log.d(TAG, "readRouteFile.size:" + sb.length());
                return sb.toString();
            }catch(IOException e){
                Log.e(TAG, "readRouteFile.IOException:" + e.getMessage());
            }

        }else{
            Log.e(TAG, "readRouteFile.!exist:" + sFile);
        }

        return null;
    }//readRouteFile

0

There are 0 answers