android 关于手机文件

1、Context.getExternalFilesDir()和Context.getExternalCacheDir():

通过Context.getExternalFilesDir()方法可以获取到 SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据。

通过Context.getExternalCacheDir()方法可以获取到 SDCard/Android/data/你的应用包名/cache/目录,一般存放临时缓存数据。

如果使用上面的方法,当你的应用在被用户卸载后,SDCard/Android/data/你的应用的包名/ 这个目录下的所有文件都会被删除,不会留下垃圾信息。
而且上面二个目录分别对应 设置->应用->应用详情里面的”清除数据“与”清除缓存“选项
如果要保存下载的内容,就不要放在以上目录下。

当我们卸载应用的时候,系统会把data/应用包名下面的files目录和cahe目录全部自动删除掉。但是如果我们把文件下载在SD卡的根路径,系统不会帮我们回收,需要用户手动删除,因此,优秀的程序员都应考虑良好的用户体验,即使由于某种原因卸载了我们的应用,我们还是应该在对应用数据位置进行最优的存储:临时数据存放在cache目录下,持久化的数据存储在files;

2、context.getExternalCacheDir() 、 context.getCacheDir()

相同点:
1、相同点:都可以做app缓存目录。
2、app卸载后,两个目录下的数据都会被清空。
不同点:
1、目录的路径不同。前者的目录存在外部SD卡上的。后者的目录存在app的内部存储上。
2、前者的路径在手机里可以直接看到。后者的路径需要root以后,用Root Explorer 文件管理器才能看到。
需要注意的事项:
1、由于context.getExternalCacheDir() 的目录存在外部SD卡上的,所以在使用这个方法的时候要判断外部SD卡的状态是否可用。

public File getCacheDir(Context context, String uniqueName) {
        String cachePath;
      if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            cachePath = context.getExternalCacheDir().getPath();
        } else {
            cachePath = context.getCacheDir().getPath();
        }

        return new File(cachePath + File.separator + uniqueName);
    }

总结:

context.getFilesDir:
/data/data/application package/files
context.getCacheDir:
/data/data/application package/cache

context.getExternalCacheDir:
/storage/emulated/0/Android/data/application package/cache
context.getExternalFilesDir:
/storage/emulated/0/Android/data/application package/files

Environment.getExternalStorageDirectory:
/storage/emulated/0

SD卡上创建文件时以下为等价:

new File(Environment.getExternalStorageDirectory(), "/Android/data/" + this.getPackageName() + "/cache")
//等价于
new File(this.getExternalCacheDir(), "")

结果都为:
/storage/emulated/0/Android/data/com.example.administrator.downloadimgdemo/cache

获取路径的字符串:


String path = File.getPath();
//或者
String absolutePath = File.getAbsolutePath();

示例:

 /*获取结果:  /data/data/<application package>/files   */
//:/data/data/com.example.administrator.downloadimgdemo/files
        Log.e("MainActivity", "getFilesDir====" + getFilesDir());

        /*获取结果:  /data/data/<application package>/cache   */
//:/data/data/com.example.administrator.downloadimgdemo/cache
        Log.e("MainActivity", "getCacheDir====" + getCacheDir());
//:/data/data/com.example.administrator.downloadimgdemo/cache
        Log.e("MainActivity", "getCacheDir.getPath====" + getCacheDir().getPath());
//:/data/data/com.example.administrator.downloadimgdemo/cache
        Log.e("MainActivity", "getCacheDir.getAbsolutePath====" + getCacheDir().getAbsolutePath());

    /* 获取结果为:/storage/emulated/0/Android/data/<application package>/cache   */
//:/storage/emulated/0/Android/data/com.example.administrator.downloadimgdemo/cache
        Log.e("MainActivity", "getExternalCacheDir====" + getExternalCacheDir());
//:/storage/emulated/0/Android/data/com.example.administrator.downloadimgdemo/cache
        Log.e("MainActivity", "getExternalCacheDir.getPath====" + getExternalCacheDir().getPath());
//:/storage/emulated/0/Android/data/com.example.administrator.downloadimgdemo/cache
        Log.e("MainActivity", "getExternalCacheDir.getAbsolutePath====" + getExternalCacheDir().getAbsolutePath());

 /* 获取结果为:/storage/emulated/0/Android/data/<application package>/files   */
//:/storage/emulated/0/Android/data/com.example.administrator.downloadimgdemo/files
        Log.e("MainActivity", "getExternalFilesDir====" + getExternalFilesDir(""));
//:/storage/emulated/0/Android/data/com.example.administrator.downloadimgdemo/files/a
        Log.e("MainActivity", "getExternalFilesDir====" + getExternalFilesDir("a"));

//获取sd卡根目录,跟应用的是否卸载无关。
        Log.e("MainActivity", "getExternalStorageDirectory====" + Environment.getExternalStorageDirectory());
        //:/storage/emulated/0
        Log.e("MainActivity", "getExternalStoragePublicDirectory====" + Environment. getExternalStoragePublicDirectory(""));
        //:/storage/emulated/0


        Log.e("MainActivity", "getDataDirectory====" + Environment.getDataDirectory());//:/data
        Log.e("MainActivity", "getDownloadCacheDirectory====" + Environment.getDownloadCacheDirectory());//:/cache
        Log.e("MainActivity", "getRootDirectory====" + Environment.getRootDirectory());//:/system

context.getExternalFilesDir(Environment.DIRECTORY_MOVIES):获取手机内存卡上视频路径。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值