自從KitKat以來,我在Android和SD卡上讀了很多東西。但最終,我找不到明確的全球答案,所以我會試着從你們那裏得到相關的答案。有沒有什麼辦法可以在Android上的SD卡上隨意寫字?
我想編寫一個文件,文本文件,在SD卡上的任意位置。
讓我們專注於Android的6這個簡單的例子。寫權限出現在清單
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我有這樣的helper方法
private void writeFile(File parent)
{
try
{
File f = new File(parent, "test.txt");
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(f));
outputStreamWriter.write("test");
outputStreamWriter.close();
Log.i(TAG, "File created : " + f.getAbsolutePath());
}
catch (IOException e)
{
Log.e(TAG, "File write failed", e);
}
}
而且在測試活動:
if (Build.VERSION.SDK_INT >= 23)
{
if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MainActivity.this, new String[] {
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.READ_EXTERNAL_STORAGE }, 1);
}
else
{
File path = getExternalFilesDirs(Environment.DIRECTORY_PICTURES)[1];
writeFile(path);
path = getExternalFilesDirs(null)[1];
writeFile(path);
writeFile(new File("/storage/3635-3533/"));
}
}
/\ 是爲第三測試的路徑!通過檢索正確的SD卡路徑硬編碼在主體這裏 /!\
首先推出,permission asked,一切正常(對不起法國設備)
第二次發射,這裏是結果
10-03 12:40:10.414: I/TESTSDCARD(26006): File created : /storage/3635-3533/Android/data/com.example.testsdcard/files/Pictures/test.txt
10-03 12:40:10.415: I/TESTSDCARD(26006): File created : /storage/3635-3533/Android/data/com.example.testsdcard/files/test.txt
10-03 12:40:10.426: E/TESTSDCARD(26006): File write failed
10-03 12:40:10.426: E/TESTSDCARD(26006): java.io.FileNotFoundException: /storage/3635-3533/test.txt: open failed: EACCES (Permission denied)
10-03 12:40:10.426: E/TESTSDCARD(26006): at libcore.io.IoBridge.open(IoBridge.java:459)
10-03 12:40:10.426: E/TESTSDCARD(26006): at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
10-03 12:40:10.426: E/TESTSDCARD(26006): at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
10-03 12:40:10.426: E/TESTSDCARD(26006): at com.example.testsdcard.MainActivity.writeFile(MainActivity.java:61)
10-03 12:40:10.426: E/TESTSDCARD(26006): at com.example.testsdcard.MainActivity.onCreate(MainActivity.java:50)
10-03 12:40:10.426: E/TESTSDCARD(26006): at android.app.Activity.performCreate(Activity.java:6367)
10-03 12:40:10.426: E/TESTSDCARD(26006): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
10-03 12:40:10.426: E/TESTSDCARD(26006): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2397)
10-03 12:40:10.426: E/TESTSDCARD(26006): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2504)
10-03 12:40:10.426: E/TESTSDCARD(26006): at android.app.ActivityThread.access$900(ActivityThread.java:165)
10-03 12:40:10.426: E/TESTSDCARD(26006): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)
10-03 12:40:10.426: E/TESTSDCARD(26006): at android.os.Handler.dispatchMessage(Handler.java:102)
10-03 12:40:10.426: E/TESTSDCARD(26006): at android.os.Looper.loop(Looper.java:150)
10-03 12:40:10.426: E/TESTSDCARD(26006): at android.app.ActivityThread.main(ActivityThread.java:5546)
10-03 12:40:10.426: E/TESTSDCARD(26006): at java.lang.reflect.Method.invoke(Native Method)
10-03 12:40:10.426: E/TESTSDCARD(26006): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
10-03 12:40:10.426: E/TESTSDCARD(26006): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
10-03 12:40:10.426: E/TESTSDCARD(26006): Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
10-03 12:40:10.426: E/TESTSDCARD(26006): at libcore.io.Posix.open(Native Method)
10-03 12:40:10.426: E/TESTSDCARD(26006): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
10-03 12:40:10.426: E/TESTSDCARD(26006): at libcore.io.IoBridge.open(IoBridge.java:445)
10-03 12:40:10.426: E/TESTSDCARD(26006): ... 16 more
正如你所看到的,使用getExternalFilesDirs
工作兩個測試(不要驚訝,它應該到) 但第三個嘗試在SD卡的根目錄寫入失敗。
我做錯了嗎?有什麼方法可以執行此操作?希望不使用存儲訪問框架,這在我的應用上下文中對我來說是一個真正的痛苦。
旁邊,並存儲訪問框架允許沒有一個巨大的代碼量上的文件執行每個基本操作(寫,刪除,重命名......)?
沒有根目錄,你沒有對'/ storage'的寫入權限 –
你是指SD卡? – ToYonos
嗯,有些設備沒有「SD卡」,所以我只是指文件系統 –