尽管授予了权限,但读取文件会导致崩溃

时间:2016-08-11 13:00:00

标签: android android-manifest bufferedreader android-permissions

我有一个名为“kaAllNodesNumbered.txt”的文本文件,它保存在名为“database”的目录下,该目录保存在externalStorage中。我按如下方式访问此文本文件:

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

上面代码中的if语句返回true,调用方法populate(this, file)。这种方法的主体如下所示。问题是,虽然文本文件“kaAllNodesNumbered.txt”存在,并且清单文件中授予了所需的权限,如下所示,但是,当我运行应用程序时,下面的代码中指示的第38行导致NPE,我收到警告如下所示。

为什么我收到此类错误和警告,尽管文件存在并且授予了权限?

填入

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  <<<<=============
    ...
    ...
    ...
}

错误和警告

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)

权限

    </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>

1 个答案:

答案 0 :(得分:0)

正如日志所说kaAllNodes.txt: open failed: **EACCES (Permission denied)** - 使用android marshmallow及以上时,您需要在从外部存储器(And for more actions)读取/写入时。

现在要获得权限,您需要调用Android的权限活动并在onActivityResult内执行您的代码。(如何实施指南here