Android 内部存储,外部存储使用范围和介绍

来源:互联网 发布:女生双肩包推荐 知乎 编辑:程序博客网 时间:2024/05/21 04:41

首先参考API文档:
Android/android-sdk/docs/training/basics/data-storage/files.html

内部存储

Internal storage
- It’s always available.
- Files saved here are accessible by only your app by default.
- When the user uninstalls your app, the system removes all your app’s files from internal storage.
Internal storage is best when you want to be sure that neither the user nor other apps can access your files.

        File filesDir = getFilesDir();        Log.i(TAG, "file_dir=" + filesDir);// ~/data/data/lbb.demo.first/files        File cacheDir = getCacheDir();        Log.i(TAG, "cacheDir=" + cacheDir); //~/data/data/lbb.demo.first/cache        File directory_qd = new File(getFilesDir(), "Conf.json");        if (!directory_qd.exists()) {            try {                directory_qd.createNewFile();            } catch (IOException e) {                e.printStackTrace();            }        }

内部存储,别的应用访问不到,同时会随着应用的卸载而卸载,同时默认情况下
- sharedpreferences: ~/data/data/lbb.demo.first/shared_prefs
- 数据库: ~/data/data/lbb.demo.first/databases
所以也会随着应用的卸载而卸载。

外部存储

简介

External storage
- It’s not always available, because the user can mount the external storage as USB storage and in some cases remove it from the device.
- It’s world-readable, so files saved here may be read outside of your control.
- When the user uninstalls your app, the system removes your app’s files from here only if you save them in the directory from getExternalFilesDir().
External storage is the best place for files that don’t require access restrictions and for files that you want to share with other apps or allow the user to access with a computer.

        //应用的私有外部存储,随着应用的卸载而卸载        File externalFilesDir = getExternalFilesDir("");        Log.i(TAG, "externalFileDir = " + externalFilesDir); // ~/storage/emulated/0/Android/data/lbb.demo.first/files        File externalCacheDir = getExternalCacheDir();        Log.i(TAG, "externalCacheDir = " + externalCacheDir); //~/storage/emulated/0/Android/data/lbb.demo.first/cache        //公共目录,不会随着应用的卸载而卸载        File sdCard = Environment.getExternalStorageDirectory();        File directory_pic1 = new File(sdCard, "Pictures");        Log.i(TAG, "directory_pic1=" + directory_pic1);// ~/storage/emulated/0/Pictures        //上面那种写法的简便方式        File directory_pic2 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);        Log.i(TAG, "directory_pic2=" + directory_pic2);// ~/storage/emulated/0/Pictures        //如果要对外部存储进行读写,那么最好判断一下,因为有时候外部存储不是有效的。        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {            File newFile = new File(Environment.getExternalStorageDirectory(), "Conf.json");            if (!newFile.exists()) {                try {                    boolean result = newFile.createNewFile();                    Log.d(TAG, "result: " + result);                } catch (IOException e) {                    e.printStackTrace();                }            }        }

外部存储不会随着应用的卸载而卸载,除了getExternalFilesDir和getExternalCacheDir目录(这个其实算是应用的专有外部存储目录,另一方面这两个目录其实可以被别的应用访问得到的,别的应用只要拿到这两个目录的路径即可),如果要对外部存储进行读写,那么最好判断一下,因为有时候外部存储不是有效的.
Note: When the user uninstalls your app, the Android system deletes the following:
- All files you saved on internal storage
- All files you saved on external storage using getExternalFilesDir().

外部存储读写权限

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

/storage/sdcard0
/sdcard
/mnt/sdcard
/storage/emulated/0/
表示的都是外部存储,有什么区别呢?

/storage/emulated/0/: to my knowledge, this refers to the “emulated MMC” (“owner part”). Usually this is the internal one. The “0” stands for the user here, “0” is the first user aka device-owner. If you create additional users, this number will increment for each.
/sdcard/: According to a comment by Shywim, this is a symlink to…
/mnt/sdcard (Android < 4.0)
/storage/sdcard0 (Android 4.0+)

外置SD卡

现在有很多手机内置有一个存储空间,同时还支持外置sd卡插入,这样通过Environment.getExternalStorageDirectory()方法获取到的就是内置存储卡的位置,需要获取外置存储卡的路径就比较麻烦

参考一下这里吧:Android获取系统外置存储卡路径的方法

0 0
原创粉丝点击