Android 如何使用Environment.getExternalStorageDirectory()

2023-12-22

我该如何使用Environment.getExternalStorageDirectory()从SD卡读取存储的图像还是有更好的方法?


Environment.getExternalStorageDirectory().getAbsolutePath()

为您提供 SDCard 的完整路径。然后,您可以使用标准 Java 执行正常的文件 I/O 操作。

这是写入文件的简单示例:

String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "myFile.txt";

// Not sure if the / is on the path or not
File f = new File(baseDir + File.separator + fileName);
f.write(...);
f.flush();
f.close();

Edit:

哎呀 - 你想要一个阅读的例子......

String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "myFile.txt";

// Not sure if the / is on the path or not
File f = new File(baseDir + File.Separator + fileName);
FileInputStream fiStream = new FileInputStream(f);

byte[] bytes;

// You might not get the whole file, lookup File I/O examples for Java
fiStream.read(bytes); 
fiStream.close();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 如何使用Environment.getExternalStorageDirectory() 的相关文章