我如何使用Environment.getExternalStorageDirectory()从SD卡读取存储的图像或有更好的方法吗?
deb*_*cey 81
Environment.getExternalStorageDirectory().getAbsolutePath()
Run Code Online (Sandbox Code Playgroud)
为您提供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();
Run Code Online (Sandbox Code Playgroud)
编辑:
哎呀 - 你想要一个阅读的例子......
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();
Run Code Online (Sandbox Code Playgroud)
hal*_*ate 36
但请记住,getExternalStorageDirectory()在某些手机上无法正常工作,例如我的Motorola razr maxx,因为它有2张卡/ mnt/sdcard和/ mnt/sdcard-ext - 分别用于内部和外部SD卡.你每次都会得到/ mnt/sdcard回复.谷歌必须提供一种处理这种情况的方法.因为它使许多SD卡识别应用程序(即卡备份)在这些手机上惨遭失败.
| 归档时间: |
|
| 查看次数: |
142407 次 |
| 最近记录: |