[英]Can't read text file from the data folder or documents folder in the phone's internal storage
[英]Can't write text files to documents folder in Android
我有一個應用程序,我正在嘗試將熱像儀中的溫度值寫入文本文件。
昨天,我能夠解決這個問題: 如何在 Android 中編寫並不斷更新文本文件,盡管重新安裝后我不再能夠將文件寫入文檔目錄。
我收到以下異常:
W/System.err:java.io.FileNotFoundException:/storage/emulated/0/Documents/1658486312697.txt:打開失敗:EPERM(不允許操作)
編寫代碼:
long time = System.currentTimeMillis();
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
try {
File outFile1 = new File(path+"/"+String.valueOf(time)+".txt");
Log.e("outFile1:", outFile1.getAbsolutePath());
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile1,true), "utf-8"), 10240);
for (int i = 10; i < 384*288+10; i++) {
out.write(temperature1[i] + "\r\n");
}
out.flush();
out.close();
} catch (Exception e){
e.printStackTrace();
}
AndroidManifest.xml
上的權限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
我目前正在 Android 12 設備上進行測試,我想知道為什么我不能將我的文本文件保存到文檔中,以及需要做些什么才能以正確的方式保存它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.