0

Since the release of android 11 i'm facing several problems that i didn't have in android 10 the main one is the file reading and writing system i'm using the permissions <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> and <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> but when I'm going to use FileInputStream to read a json file, I always get this error:

11443-11443 W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Documents/Brizas-app/configs/database.json: open failed: EACCES (Permission denied)
11443-11443 W/System.err: at libcore.io.IoBridge.open(IoBridge.java:492)
11443-11443 W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:160)
11443-11443 W/System.err: at com.brizaloka.brizasapp.ui.gadgets.fetchJson.loadJSONFromAsset(fetchJson.java:79)
11443-11443 W/System.err: at com.brizaloka.brizasapp.ui.api_insert.Insert.onCreateView(Insert.java:112)
11443-11443 W/System.err: at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
11443-11443 W/System.err: at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
11443-11443 W/System.err: at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
11443-11443 W/System.err: at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
11443-11443 W/System.err: at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
11443-11443 W/System.err: at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
11443-11443 W/System.err: at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
11443-11443 W/System.err: at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
11443-11443 W/System.err: at android.os.Handler.handleCallback(Handler.java:938)
11443-11443 W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
11443-11443 W/System.err: at android.os.Looper.loop(Looper.java:236)
11443-11443 W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7864)
11443-11443 W/System.err: at java.lang.reflect.Method.invoke(Native Method)
11443-11443 W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
11443-11443 W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)

I'm trying everything to use and I even used the permission <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/> but it's not accepted in the play store so I removed it, and it's all I did to read the files on android 10 using android:requestLegacyExternalStorage="true" and android:allowBackup="true" I honestly don't know where the error is since in the previous apis they were working perfectly

Function I use to read a json file:

public static JSONObject loadJSONFromAsset(Context context, String filename) {
        JSONObject json = null;
        File file = new File(filename);
        try {
            FileInputStream is = new FileInputStream(file);

            int size = is.available();

            byte[] buffer = new byte[size];

            is.read(buffer);

            is.close();

             json = new JSONObject(new String(buffer, "UTF-8"));


        } catch (IOException | JSONException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;

    }
Ian Pablo
  • 21
  • 2
  • try accessing file name with https://stackoverflow.com/a/58489112 – Jaydeep Devda Aug 25 '21 at 11:16
  • You did not tell how your .json file landed in that directory. It looks as if the file is not created and written by your app. – blackapps Aug 25 '21 at 11:28
  • `loadJSONFromAsset()` From assets? From storage you mean. – blackapps Aug 25 '21 at 11:29
  • Try to create a json file in that directory with your app and see that you will succeed – blackapps Aug 25 '21 at 11:32
  • I wrote the file manually just for a test, I guarantee it is present – Ian Pablo Aug 25 '21 at 11:34
  • I used context.getExternalFilesDir but there is a problem, I want it to be saved in android's Documents folder, and getExternalFilesDir is saved in android's data folder – Ian Pablo Aug 25 '21 at 11:53
  • No you did not use getExternalFilesDir() as that would never have given the path in the error message: `java.io.FileNotFoundException: /storage/emulated/0/Documents/Brizas-app/configs/database.json: open failed:` – blackapps Aug 25 '21 at 14:41
  • `I wrote the file manually just for a test, I guarantee it is present ` Manually? What would be that? We wanna know if it was your app or another app or yet something else. Its irrelevant that the file is present. Of course it is. But to who belongs the file? – blackapps Aug 25 '21 at 14:44

0 Answers0