在我的知识范围内,安卓的存储设备有几种
- ROM(read only menory),操作系统就是放在这一块里面,这块存储空间不能被随便读写,用来放音乐视频就更别想了
- SDCard,这个有点麻烦。有些手机需要插内存卡才能拍照,对于这一部分机子来说,SDCard就是你插上去的内存卡,容量是可以改变的。有些手机不需要插内存卡就可以拍照更有反人类的设计狮甚至把手机设计成是不支持内存卡,对于这一部分手机来说,SDCard是内置存储设备,是焊接在主板上的一块Flash芯片,想扩容?当时买手机的时候为啥不对自己狠点?
- External_SDCard,这个就简单多了。这个概念常见于上面第二点中的第二种手机上面。如果支持内存卡扩展,那么这张内存卡就是External_SDCard了。
- USB_Storage,用OTG挂一个U盘什么的就是了。
1、我最喜欢用,最直接,最不负责任的一种:直接在SD卡上写文件
通过Environment.getExternalStorageDirectory()一个File对象,通过File.getAbsoluatePath()方法可获得SD卡的绝对路径
String path = Environment.getExternalStorageDirectory().getAbsoluatePath();
path的结果为“/storage/sdcard0”
注意后面是没有'/'的,想要在SD卡的根目录下面再新建一个文件夹,使用mkdirs函数即可
File dir = new File(path + "HelloWorld");
if(!dir.exist()) {
dir.mkdirs();
}
想要在SD卡的根目录下面新建一个文件,使用File.createNewFile()方法或者直接打开一个FileOutputStream并往里里面写入一些东西
File file = new File(path + "/HelloWorld.txt");
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
/*****************分割线*****************/
try {
FileOutputStream fos = new FileOutputStream(path + "/HelloWorld.txt");
fos.write("Hello World!".getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
通过上面的方式创建的文件和文件夹系统或者第三方软件都不能判断这是用户的文件还是应用创建的文件因而当应用被卸载的时候,这些文件和文件夹将一直被保存,对有些用户来说,这就是垃圾
2、在安卓系统给你的空间上面写文件,这是一种比较好的方式,如果应用运行过程中产生的所有文件都被放到这些目录下面,那么应用被卸载的时候,用户将再也找不到这个应用的任何痕迹
安卓系统为每一个安装在他上面的应用都准备了一个私人文件夹,这个文件夹的路径为
/data/data/<package name>
如果手机没有root,那么在这个文件夹中的内容除了应用自己,以及通过应用对外提供了访问接口之外没有人能够访问到这些文件,账户信息等通常存储在这里面。在安卓的应用管理器上面会有两个按钮,一个是清除数据,一个是清除缓存,缓存还能够通过第三方软件清理掉,如果把账户信息保存为缓存就会引发一大堆不必要的麻烦了。
安卓中通过每一个Context对象可以使用下面四个比较重要的方法:
Context.FilesDir();
Context.getCacheDir();
Context.getExternalCacheDir();
Context.getExternalFilesDir(String type);
直接上例子吧
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsoluatePath() + "/Paths.txt");
fos.write(("Context.FilesDir() : " + Main.this.getFilesDir().getAbsolutePath() + "\n").getBytes());
fos.write(("Context.getCacheDir() : " + Main.this.getCacheDir().getAbsolutePath() + "\n").getBytes());
fos.write(("Context.getExternalCacheDir() : " + Main.this.getExternalCacheDir().getAbsolutePath() + "\n").getBytes());
fos.write(("Context.getExternalFilesDir(\HelloWorld\") : " + Main.this.getExternalFilesDir("HelloWorld").getAbsolutePath() + "\n").getBytes());
fos.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
在安卓设备上运行这个程序,运行过后在SD卡根目录下找到Path.txt文件,打开内容如下:
Context.FilesDir() : /data/data/com.lkm.helloandroid/files
Context.getCacheDir() : /data/data/com.lkm.helloandroid/cache
Context.getExternalCacheDir() : /storage/sdcard0/Android/data/com.lkm.helloandroid/cache
Context.getExternalFilesDir("HelloWorld") : /storage/sdcard0/Android/data/com.lkm.helloandroid/files/HelloWorld
其中com.lkm.helloandroid为我的应用程序包名
通过Context.FilesDir()得到的是ROM中应用程序私有文件夹中名为files的文件夹的绝对路径,往files文件夹中写文件,只能通过清除数据才能清除掉通过Context.getCacheDir()可得到cache文件夹的绝对路径,往cache文件夹中写入的数据通过清除缓存就可以清理掉通过Context.getExternalCacheDir(),Context.getExternalFilesDir(String type)两个方法得到在SD卡上的files文件夹跟cache文件夹的绝对路径。使用方法跟上面一样,只是用户可以通过文件浏览器直接访问这些文件
在安4.4版本以后,应用程序对外置储存卡的读写进行了限制,应用程序只能在自己的私有文件夹中读写数据