【发布时间】:2018-11-15 11:09:33
【问题描述】:
我正在使用这种方法,在SO中发现很多问题:
public void generateNoteOnSD(Context context, String sFileName, String sBody) {
try {
File root = new File(Environment.getExternalStorageDirectory()+File.separator+"folder", "AGL");
if (!root.exists()) {
root.mkdirs();
}
File gpxfile = new File(root, sFileName);
FileWriter writer = new FileWriter(gpxfile);
writer.append(sBody);
writer.flush();
writer.close();
if(gpxfile.exists()){
Toast.makeText(context, "OK", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "KO", Toast.LENGTH_SHORT).show();
}
Almacen.setRutaFichero(gpxfile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
我的 AndroidManifest 中有这个:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
一切似乎都很好,没有抛出异常,正确的 Toast 显示在屏幕上......但我转到文件应该所在的文件夹......但它不在那里。我正在摸不着头脑,因为这种方法好几次都行得通!我正在使用一个编写日志和所有内容的库......并且带有日志的文件在那里......
有什么帮助吗?
【问题讨论】:
-
在SD卡上创建文件需要
DocumentTree的访问权限 -
你能详细说明一下吗?
-
是否已授予运行时写入存储权限?
-
我使用的是 API 16 设备,不会在其他设备上运行,所以不需要
-
如果您在 APi 23+ 上运行,那么您需要
DocumentTree权限,也称为 SAF 更多信息 Here