要搞清楚文件存储首先需要知道内存 和外存的定义

:

在任何时候都是可用的,

默认情况你存储的文件只有你自己的应用可用访问

当用户卸载app时候,系统删除所有内存中保存的文件

 

外存:不总是可以使用的,当用户插上电脑将手机作为u盘时候,你的应用将不可以使用这些文件。并且用户可用删除的。

外存中的文件是全局可读的,文件有时候可以被用户删除。

当用户卸载app时候,卸载的文件仅仅是函数getExternalFileDir得到目录下的文件。其他文件不会删除。

外存中的文件应该是可以和其他app可以访问,也允许用户去访问的。

 

App安装默认是在内存中,你也可以在Mainfest文件中指定安装位置通过android:installLocation属性。

 

读写外部存储文件需要在mainfest中声明权限

写权限

<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    ...
</manifest>

读权限:

<manifest ...>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    ...
</manifest>

在内存中保存文件不需要权限,你的应用总是可以读写内存中的文件。

 

保存文件在内存中你需要得到内存的目录:

getFilesDir()app目录

 

getCacheDir()app临时缓存文件的目录,你需要设定一个文件缓存大小,当你不需要文件的时候一定要删除他,或者你app 内存不够用时候,也可能自动删除你缓存中的文件。

 

创建文件的方法:

File file = new File(context.getFilesDir(), filename);

 

你也可以通过OpenFileOutput得到FileOutputStream类去写入文件

String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;

try {
  outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
  outputStream.write(string.getBytes());
  outputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}

 

创建缓存文件通过url

public File getTempFile(Context context, String url) {
    File file;
    try {
        String fileName = Uri.parse(url).getLastPathSegment();
        file = File.createTempFile(fileName, null, context.getCacheDir());
    catch (IOException e) {
        // Error while creating file
    }
    return file;
}

 

注意:若果其他应用知道你应用的包名,应该可以访问你的内存中的文件,你需要设置文件属性为可读状态,如果你设置MODE_PRIVAT 其他app将永远访问不到你的文件。

 

保存文件在外存中:

因为外存不总是可用的状态,所以你在读写外存文件的时候需要提前获取外存的状态

如下代码:

/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) ||
        Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;
}

 

在外部存储中你可以保存文件的位置有两个:

公共文件: 这里的文件可以被用户和其他app自由访问,卸载你的app也不会卸载这里的文件,会留下来。 例如文件下载目录  图像截图目录。

私有文件:用户卸载你的应用时,也会卸载这里的数据。因为他们在外存上,所以可以被其他app和用户访问。这个文件放置临时媒体文件,或者你的应用下载的资源文件。

 

得到外部存储公共目录的文件夹通过getExternalStoragePublicDirectory() ,你需要指定文件类型,你要保存的数据的文件类型。例如DIRECTORY_MUSIC 或者DIRECTORY_PICTURES

得到用户的相册目录

public File getAlbumStorageDir(String albumName) {
    // Get the directory for the user's public pictures directory. 
    File file = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), albumName);
    if (!file.mkdirs()) {
        Log.e(LOG_TAG, "Directory not created");
    }
    return file;
}

 

如果你保存的文件对你的app来说是私有的,你需要调用getExternalFilesDir()

创建自己app私有的相册

public File getAlbumStorageDir(Context context, String albumName) {
    // Get the directory for the app's private pictures directory. 
    File file = new File(context.getExternalFilesDir(
            Environment.DIRECTORY_PICTURES), albumName);
    if (!file.mkdirs()) {
        Log.e(LOG_TAG, "Directory not created");
    }
    return file;
}

 

注意:当你不确定目录名传入null 你获取的目录是外存中你应用的根目录

在你知道目录的时候需要传入文件类型,这样系统扫描的时候,可以正确识别你的文件类型。

 

你可以调用File类的getFreeSpace 和getTotalSpace 来得到当前文件目录可用的空间。

在外存中你可以调用file.delete()删除文件。

 

文件在内存中你可以调用context.deleteFile(fileName);来删除文件

 

注意:卸载应用的时候 会删除的目录有内存中的你app的文件,以及外部存储中getExternalFilesDir(). 目录下的文件。