0

I am developing my first app and I have to read a txt file from memory card. I cant seem to get the read permission for any of the directories or files in /storage. I am using FileBrowserActivity from https://github.com/vaal12/AndroidFileBrowser to get a GUI for file selection. I have tried from the GUI and just passing the path to the file but none of them work. Here is what I have:

InputStream is = null;
        File dir = Environment.getExternalStorageDirectory();
        File f_path = new File(filepath);//dir,
        Log.i(TAGraph, "file exists: " + f_path.exists() + " isDirectory: " + f_path.isDirectory() + " read: " + f_path.canRead());
        if(f_path.exists() && !f_path.isDirectory() && f_path.canRead()) {
            Log.i(TAGraph, "file from user1");
            is = new BufferedInputStream(new FileInputStream(f_path));
            Log.i(TAGraph, "file from user2");
        }
        else{
            is = getAssets().open("DATA.txt");
            Log.i(TAGraph, "file from Assets 3");
        }

From Logcat:

11-24 16:15:29.636 31337-31337/com.elcheapo.ashplot I/tagraph: on FILE try1. filepath: /storage/DATA.txt
11-24 16:15:29.643 31337-31337/com.elcheapo.ashplot I/tagraph: file exists: true isDirectory: false read: false
11-24 16:15:29.643 31337-31337/com.elcheapo.ashplot I/tagraph: file from Assets 3

AndroidManifest.xml

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

I have tried with only read, both read and write and with and without SdkVersion.

I can always get the file from assets just not from the storage. The file is there. I have tried testing on the emulator and on LG G5. I am getting the filepath from the mainActivity. I have checked and the path passes through fine.(Dont know if Activity to Activity permissions change)

  • I am not aware of any Android device ever created where a `/storage/DATA.txt` file is even possible, let alone one that you can access. I suspect that your code for obtaining `filepath` has issues. – CommonsWare Nov 25 '16 at 00:32
  • @CommonsWare has a point. I'd assumed you'd redacted that for some reason, given that `exists()` is returning `true`. Let us know if the linked duplicate question does not solve your problem, and one of us can remove the duplicate closure. – Mike M. Nov 25 '16 at 00:36
  • Hey thanks. Yes that is the method that I wanted, but specifically this http://stackoverflow.com/questions/33162152/storage-permission-error-in-marshmallow – pointnotfoe Nov 25 '16 at 06:35

0 Answers0