简体   繁体   English

Android对SD卡的写入权限

[英]Android write access to SD card

I am trying to write to External secondary storage (sd card), but it looks like I do not have the permission. 我正在尝试写入外部辅助存储(SD卡),但似乎我没有此权限。

According to Android: 根据Android:

" Secondary external storage must not be writable by apps, except in package-specific directories as allowed by synthesized permissions. " 除非应用程序综合许可所允许的特定于软件包的目录,否则应用程序不得写入辅助外部存储。

So this wont work: 所以这行不通:

try {
    String sdCard = System.getenv("SECONDARY_STORAGE");
    FileWriter writer = new FileWriter(new File(sdCard, "test.txt"));
    writer.write("a test");
    writer.flush();
    writer.close();
} catch (IOException e) {
    Log.d(e.toString());
    //java.io.FileNotFoundException: /storage/extSdCard/test.txt: open failed: EACCES (Permission denied) 
}

I have the permission in AndroidManifest.xml <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 我在AndroidManifest.xml中具有权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

and Environment.getExternalStorageDirectory() wont work since I am trying to write to removable sd card not the phone memory. Environment.getExternalStorageDirectory()无法正常工作,因为我试图写入可移动sd卡而不是手机内存。

So, if apps supposed to be prohibited to write to secondary storage, how can apps like File Manager write to sd cards? 因此,如果应该禁止将应用程序写入辅助存储,则文件管理器等应用程序如何将数据写入SD卡?

So this wont work: 所以这行不通:

Among other reasons, I am not aware of a requirement for devices to have a SECONDARY_STORAGE environment variable. 除其他原因外,我不知道设备必须具有SECONDARY_STORAGE环境变量。

I have the permission in AndroidManifest.xml 我在AndroidManifest.xml中有权限

That is for external storage , not removable storage . 那是用于外部存储 ,而不是可移动存储

how can apps like File Manager write to sd cards? 文件管理器等应用程序如何写入SD卡?

They are relying on users helping them via the Storage Access Framework . 他们依靠用户通过Storage Access Framework帮助他们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM