Android文件存储路径

一、最佳方案

Google提供了最佳的文件存储方案:通过context.getExternalFilesDir(dir)获取外部存储路径,通过context.getFilesDir()获取内部存储路径;通过context.getExternalCacheDir(dir)获取外部缓存路径,通过context.getCacheDir()获取内部存储路径(dir为自定义文件夹,dir可为null)。

二、存储区别简明示意图如下:

区别

方法

备注

 externalstorage外部存储

Environment.getExternalStorageDirectory()

SD根目录:/mnt/sdcard/(6.0后写入需要用户授权)

context.getExternalFilesDir(dir)

路径为:/mnt/sdcard/Android/data/<packagename>/files/… 

context.getExternalCacheDir()

路为:/mnt/sdcard/Android/data/<packagename>/cach/…

internalstorage内部存储

context.getFilesDir()

路径是:/data/data/<packagename>/files/…

context.getCacheDir()

路径是:/data/data/<packagename>/cach/…

三、使用方法

1、首先,配置SD卡读写权限:

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

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

2、明白区别

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

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

通过这四个方法,当你的应用在被用户卸载后,SDCard/Android/data/你的应用的包名/ 这个目录下的所有文件都会被删除,不会留下垃圾信息。

context.getExternalFilesDir()和context.getFilesDir()方法对应 设置->应用->应用详情里面的”清除数据“

context.getExternalCacheDir()和context.getCacheDir()方法对应 设置->应用->应用详情里面的”清除缓存“选项

3、具体代码

/**

* @param context 上下文对象
* @param dir  存储目录
* @return
*/
public static String getFilePath(Contextcontext,String dir) {
String directoryPath="";
//判断SD卡是否可用 
if(MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ) {
directoryPath=context.getExternalFilesDir(dir).getAbsolutePath() ;  

// directoryPath =context.getExternalCacheDir().getAbsolutePath();  
}else{
//没内存卡就存机身内存 
directoryPath=context.getFilesDir()+File.separator+dir;

//directoryPath=context.getCacheDir()+File.separator+dir;
}
File file = new File(directoryPath);  
if(!file.exists()){//判断文件目录是否存在  
file.mkdirs();  

return directoryPath;
}

 

/**

  * 获取app缓存路径

  * @param context

  * @return

  */

 public String getCachePath( Contextcontext ){

     String cachePath ;

     if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())

             ||!Environment.isExternalStorageRemovable()) {

         //外部存储可用

         cachePath= context.getExternalCacheDir().getPath() ;

     }else {

         //外部存储不可用

         cachePath= context.getCacheDir().getPath() ;

     }

     return cachePath;

 }

 

四、总结:

1、缓存路径context.getExternalCacheDir()   context.getCacheDir()

相同点:

1、相同点:都可以做app缓存目录。

2、app卸载后,两个目录下的数据都会被清空。

 

不同点:

1、目录的路径不同。前者的目录存在外部SD卡上的。后者的目录存在app的内部存储上。

2、前者的路径在手机里可以直接看到。后者的路径需要root以后,用Root Explorer 文件管理器才能看到。

2、存储路径context.getExternalFilesDir()和context.getFilesDir()

相同点:

1、相同点:都可以做app存储目录。

2、app卸载后,两个目录下的数据都会被清空。

不同点:

1、目录的路径不同。前者的目录存在外部SD卡上的。后者的目录存在app的内部存储上。

2、前者的路径在手机里可以直接看到。后者的路径需要root以后,用Root Explorer 文件管理器才能看到。

其中, getExternalCacheDir()与getCacheDir()的区别 和getExternalFilesDir()与getFilesDir()的区别相同,前者是在路径下自动建一个cach文件目录: /mnt/sdcard/data/<packagename >/files/cach/...,而后者需要代码创建目录。

本文参照博客:https://www.cnblogs.com/zhaoyanjun/p/4530155.html  

和  https://blog.csdn.net/losefrank/article/details/53464646  ,在此特别感谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值