0

I have already coded the below function call and is using it in one app. That is working in the same phone. Now, I copy the function to another app but it causes catch error at the line:

bR = new BufferedReader(new FileReader(gpxfile));

Anyone know why is this? And how can I inspect the error message? Noted I already added the permissions:

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

public void addPhoneEntriesFromFile(String thisfile)   {
    String aDataRow;

    // load from textfile
    try {

        String newpath;
        String subfolder = "DriveSyncFiles/_sync";
        newpath = Environment.getExternalStorageDirectory().getPath();
        if (!subfolder.equals("")) {
            newpath += "/" + subfolder;
        }
        newpath += "/";

        BufferedReader bR;
        
        if (thisfile.equals("")){
            thisfile = "myaddress.txt";
        }
        
        File gpxfile = new File(newpath, thisfile );
        myFun.ShowToast(getApplicationContext(), "begin");
        bR = new BufferedReader(new FileReader(gpxfile));

        aDataRow = "";
        Boolean stopWhile = false;
        while (((aDataRow = bR.readLine()) != null) && (stopWhile == false)) {
            if (aDataRow.equals("===")) {
                stopWhile = true;
            } else {
                Log.d("mycode", aDataRow);

            }
        }
        bR.close();
        myFun.ShowToast(getApplicationContext(), "end");
    } catch (IOException e) {
        e.printStackTrace();
    }
 
}
Mr Chan
  • 47
  • 7
  • "And how can I inspect the error message?" -- use Logcat: https://stackoverflow.com/q/23353173/115145 – CommonsWare Oct 19 '21 at 16:29
  • Check first if the file exists: if(!gpxfile.exist()) return; – blackapps Oct 19 '21 at 16:29
  • Also check first if the file is readable: if(!gpxfile.canRead()) return; – blackapps Oct 19 '21 at 16:30
  • I use this at the catch and get below error message: String info =Log.getStackTraceString(e); java.io.FileNotFoundException open failed: EACCES (Permission denied) But the file is in the same phone – Mr Chan Oct 19 '21 at 16:57
  • (!gpxfile.canRead()) is true, so the file cannot be read! But the first app is working normally. why? – Mr Chan Oct 19 '21 at 17:02
  • can I call addPhoneEntriesFromFile() in the onCreate event? – Mr Chan Oct 19 '21 at 17:04
  • I find nothing using Log.e(). Is it that adb is in wrong edition or not working correctly? – Mr Chan Oct 19 '21 at 17:05
  • (!gpxfile.exist() is false, so it exists – Mr Chan Oct 19 '21 at 17:08
  • Probably I need to fix this first: EACCES (Permission denied) by reinstalling something? Only the new app cannot read the file. The installed old app can read the same file. – Mr Chan Oct 19 '21 at 17:13
  • Yes that is quite normal for Android 11 as the owner of a file is important. A reinstalled app is no longer the owner. – blackapps Oct 19 '21 at 18:26
  • I found the problem. I make a duplicate copy of the old app and modify it. After install, it also cannot read the file. The reason is that, the phone is Miui and it has its own app permission inside. So I have to make the permission of the app for the phone too. – Mr Chan Oct 20 '21 at 02:30

1 Answers1

0

I found the problem. I make a duplicate copy of the old app and modify it. After install, it also cannot read the file. The reason is that, the phone is Miui and it has its own app permission inside. So I have to make the permission of the app for the phone too.

Mr Chan
  • 47
  • 7