我有一個應用程序(商業應用程序)在其緩存目錄中創建數據文件(csv格式)。我需要在我的應用程序中讀取這個數據文件來分析數據。如何使用我的應用程序從其他應用程序(在其私有緩存目錄中)讀取文件
但是,當我嘗試使用inputStream方法讀取文件與其絕對路徑訪問我發現權限被拒絕。
所以我想我必須改變或需要許可,但我不完全明白。我是Android開發的初學者,我閱讀內容提供商,外部存儲的一些文章,但沒有解決我的問題。你能不能幫我請...
我的代碼:?
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中產生的異常:
12月8日至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的版本,這就是爲什麼我問這是哪個版本的原因)。 – EJoshuaS