1

I have a text file named "kaAllNodesNumbered.txt" and it is saved under a directory called "database", and that directory in saved in the externalStorage. I access this text file as follows:

File file = new File(Environment.getExternalStorageDirectory() + "/database/" + "kaAllNodesNumbered.txt");
    if (file.exists()) {
    Log.i(TAG, "file.exists: " + file.exists());
    populate.populate(this, file);
  }

The if-statement in the above code, returns true and the method populate(this, file) is getting called. The body of this methods is shown below. The problem is, although the text file "kaAllNodesNumbered.txt" exists and the required permissions are granted in the manifest file as shown below, however, when I run the App, the line 38 indicated in the code below causes NPE and I receive warnings as indicated below.

Why I am receiving such errors and warnings despite the file exists and the permissions are granted?

populate

public void populate(Context context, File file) {
    this.mCtx = context;
    this.mSQLiteHelper = new SQLiteHelper(this.mCtx);
    //this.mSQLiteHelper.deleteALLRows();
    //Log.i(TAG, "total rows: " + this.mSQLiteHelper.getTotalRowsInDB());

    try {
        this.mFR = new FileReader(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    this.mBR = new BufferedReader(this.mFR); //LINE 38  <<<<=============
    ...
    ...
    ...
}

errors and warnings

java.io.FileNotFoundException: /storage/emulated/0/database/kaAllNodes.txt: open failed: EACCES (Permission denied)
08-11 14:53:37.822 25066-25066/com.example.com.ecoassistant_03 W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:452)
08-11 14:53:37.822 25066-25066/com.example.com.ecoassistant_03 W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:76)
08-11 14:53:37.822 25066-25066/com.example.com.ecoassistant_03 W/System.err:     at java.io.FileReader.<init>(FileReader.java:42)
08-11 14:53:37.822 25066-25066/com.example.com.ecoassistant_03 W/System.err:     at com.example.com.ecoassistant_03.Populator.populate(Populator.java:34)
08-11 14:53:37.822 25066-25066/com.example.com.ecoassistant_03 W/System.err:     at com.example.com.ecoassistant_03.ActMain.onOptionsItemSelected(ActMain.java:1185)
08-11 14:53:37.822 25066-25066/com.example.com.ecoassistant_03 W/System.err:     at android.app.Activity.onMenuItemSelected(Activity.java:3205)
08-11 14:53:37.822 25066-25066/com.example.com.ecoassistant_03 W/System.err:     at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
08-11 14:53:37.822 25066-25066/com.example.com.ecoassistant_03 W/System.err:     at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:147)

 java.lang.NullPointerException: lock == null
08-11 13:51:59.106 3380-3380/com.example.com.ecoassistant_03 E/AndroidRuntime:     at java.io.Reader.<init>(Reader.java:64)
08-11 13:51:59.106 3380-3380/com.example.com.ecoassistant_03 E/AndroidRuntime:     at java.io.BufferedReader.<init>(BufferedReader.java:107)
08-11 13:51:59.106 3380-3380/com.example.com.ecoassistant_03 E/AndroidRuntime:     at java.io.BufferedReader.<init>(BufferedReader.java:95)
08-11 13:51:59.106 3380-3380/com.example.com.ecoassistant_03 E/AndroidRuntime:     at com.example.com.ecoassistant_03.Populator.populate(Populator.java:38)
08-11 13:51:59.106 3380-3380/com.example.com.ecoassistant_03 E/AndroidRuntime:     at com.example.com.ecoassistant_03.ActMain.onOptionsItemSelected(ActMain.java:1182)
08-11 13:51:59.106 3380-3380/com.example.com.ecoassistant_03 E/AndroidRuntime:     at android.app.Activity.onMenuItemSelected(Activity.java:3205)

permissions

    </application>
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
manfcas
  • 1,933
  • 7
  • 28
  • 47
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • 2
    https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it – CommonsWare Aug 11 '16 at 13:00
  • Your error says `java.io.FileNotFoundException: ...... /kaAllNodes.txt` but it looks like you're trying to open `kaAllNodesNumbered.txt`, perhaps you mistyped it somewhere in your code? – CookieMonster Aug 11 '16 at 13:21

1 Answers1

0

As the log says kaAllNodes.txt: open failed: **EACCES (Permission denied)** - When working with android marshmallow and above you need to when reading/writing from external memory (And for more actions).

Now to get permissions you need to call the Android's permission activity and do your code inside onActivityResult.(How to implement guide here)

Nir Duan
  • 6,164
  • 4
  • 24
  • 38