【发布时间】:2016-08-11 21:26:04
【问题描述】:
我有一个应用程序(商业应用程序)在其缓存目录中创建一个数据文件(csv 格式)。我需要在我的应用程序中读取这个数据文件来分析数据。
但是当我尝试使用 inputStream 方法读取具有绝对路径访问权限的文件时,我发现了一个异常权限被拒绝。
所以我想我必须更改或要求许可,但我不完全理解。我是 android 开发的初学者,我阅读了一些关于 content provider, external storage 的文章,但没有解决我的问题。你能帮我吗?...
我的代码:
List<String> RowDataFile = new ArrayList<String>();
FileInputStream input = null;
String filePath = "storage/emulated/0/Android/data/com.lifescan.reveal/cache";
//String filePath = "storage/emulated/0/Android/data/vincent.gridlayout/files";
File f = new File(filePath,"/data.csv");
try {
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
for (String line = br.readLine(); line != null; line = br.readLine()) {
RowDataFile.add(line);
}
} catch (IOException e) {e.printStackTrace();}
logcat中产生的异常:
08-12 12:37:21.254 30563-30563/vincent.gridlayout W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.lifescan.reveal/cache/data .csv:打开失败:EACCES(权限被拒绝)
Android设备系统:4.4.2
和清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="vincent.heatMap">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HeatMapEdit"></activity>
</application>
</manifest>
感谢您的帮助...
【问题讨论】:
-
您能发布代码和完整的日志/异常详细信息吗?另外,这发生在哪个Android版本中?看起来您确实缺少权限,但完整的日志会有所帮助。顺便说一句,您的清单文件是否请求读取/写入外部存储的权限? (外部存储权限的确切行为因 Android 版本而异,这就是为什么我要询问发生在哪个版本上的原因)。