1.内部存储(私有的,其他程序不能获取)
a.
存:FileOutputStream fos = openFileOutput("file", MODE_PRIVATE);
读: FileInputStream fis = openFileInput("file");
文件路径:data/data/当前应用程序包名/files/文件
b.自定义路径
Sting filePath = "data/data/当前应用程序包名/文件"
FileOutputStream fos = new FileOutputStream(filePath);
File file = new File(getFileDir(), "文件"); // getFileDir() = 文件路径:data/data/当前应用程序包名/files
c.缓存文件
getCacheDir() = 文件路径:data/data/当前应用程序包名/cache
2.扩展存储(sd卡) 公有的
a.权限:
READ_EXTERNAL_STORAGE、WRITE_...、MOUNT_UNMOUNT_FILESYSTEMS(对sd卡读写和对sd卡文件夹创建删除)
b.获取当前sd卡状态
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
c.sd卡根路径
String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
File.separator = /
d.sd卡工具类
public class SDUtils{
public static void writeToSD(String dirPath, String fileName, byte[] bytes) {
if(isSdOK()) {
File dir = new File(Environment.getExternalStorageDirectory(), dirPath);
if(!dir.exists()) {
dir.mkdirs();
}
File destFile = new File(dir, fileName);
FileOutputStream fos = new FileOutputStream(destFile);
fos.write(bytes);
finally { fos.close(); }
} else {
Log.i("sd卡","sd卡异常")
}
}
private static boolean isSdOK() { ... }
public static byte[] readFromSd(String path) {
if(isSdOK()) {
FileInputStream fis = null;
ByteArrayOutputStream baos = null;
...
return baos.toByteArray();
}
}
}