获取内置目录,外置目录的路径
方案一:第三方框架PathUtils
使用https://github.com/Blankj/AndroidUtilCode
// Define versions in a single place
ext {
utilcodeVersion = '1.26.0'
}
//https://github.com/Blankj/AndroidUtilCode
implementation "com.blankj:utilcode:$rootProject.utilcodeVersion"
方案二:使用Android原生方法,自己封装
package com.zhangyu.myfilepath;
import android.app.Application;
import android.content.Context;
public class BaseApplication extends Application {
private static Context context;
private static BaseApplication instance;
public static BaseApplication getInstance(){
return instance;
}
public static Context getContext(){
return context;
}
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
instance = this;
}
}
package com.zhangyu.myfilepath;
import android.os.Environment;
import android.util.Log;
import java.io.File;
public class FileManager {
private static final String TAG = "FileManager";
private static String external_root;
private static String internal_root;
private static String sd_root;
private static FileManager fileManager;
public static FileManager get() {
fileManager = new FileManager();
return fileManager;
}
public FileManager() {
sd_root = Environment.getExternalStorageDirectory().getAbsolutePath();
external_root = BaseApplication.getInstance().getExternalFilesDir(null).getParent();
internal_root = BaseApplication.getInstance().getFilesDir().getParent();
Log.e(TAG, "sd_root: " + sd_root);
Log.e(TAG, "external_root: " + external_root);
Log.e(TAG, "internal_root: " + internal_root);
}
public String getInternalAppFilesPath() {
return internal_root + File.separator + "file";
}
public String getInternalAppCachePath() {
return internal_root + File.separator + "cache";
}
public String getInternalAppSpPath() {
return internal_root + File.separator + "shared_prefs";
}
//删除应用后,外置目录也会一并删除
public String getExternalAppFilesPath() {
return external_root + File.separator + "file";
}
public String getExternalAppCachePath() {
return external_root + File.separator + "cache";
}
/**
* 返回sd卡的根目录
* @return
*/
public String getExternalStoragePath(){
return sd_root;
}
}
三、对比结果
//使用utilcode
Log.e(TAG, "PathUtils.getExternalStoragePath(): " + PathUtils.getExternalStoragePath());
Log.e(TAG, "PathUtils.getInternalAppFilesPath(): " + PathUtils.getInternalAppFilesPath());
Log.e(TAG, "PathUtils.getInternalAppCachePath():" + PathUtils.getInternalAppCachePath());
Log.e(TAG, "PathUtils.getInternalAppSpPath():" + PathUtils.getInternalAppSpPath());
Log.e(TAG, "PathUtils.getExternalAppFilesPath():" + PathUtils.getExternalAppFilesPath());
Log.e(TAG, "PathUtils.getExternalAppCachePath():" + PathUtils.getExternalAppCachePath());
//使用自己的FileManager
Log.d(TAG, "FileManager.get().getExternalStoragePath(): " + FileManager.get().getExternalStoragePath());
Log.d(TAG, "FileManager.get().getInternalAppFilesPath(): " + FileManager.get().getInternalAppFilesPath());
Log.d(TAG, "FileManager.get().getInternalAppCachePath(): " + FileManager.get().getInternalAppCachePath());
Log.d(TAG, "FileManager.get().getInternalAppSpPath(): " + FileManager.get().getInternalAppSpPath());
Log.d(TAG, "FileManager.get().getExternalAppFilesPath(): " + FileManager.get().getExternalAppFilesPath());
Log.d(TAG, "FileManager.get().getExternalAppCachePath(): " + FileManager.get().getExternalAppCachePath());
2019-12-23 19:16:28.983 9584-9584/? E/MainActivity: PathUtils.getExternalStoragePath(): /storage/emulated/0
2019-12-23 19:16:28.983 9584-9584/? E/MainActivity: PathUtils.getInternalAppFilesPath(): /data/user/0/com.zhangyu.myfilepath/files
2019-12-23 19:16:28.983 9584-9584/? E/MainActivity: PathUtils.getInternalAppCachePath():/data/user/0/com.zhangyu.myfilepath/cache
2019-12-23 19:16:28.983 9584-9584/? E/MainActivity: PathUtils.getInternalAppSpPath():/data/user/0/com.zhangyu.myfilepath/shared_prefs
2019-12-23 19:16:28.988 9584-9584/? E/MainActivity: PathUtils.getExternalAppFilesPath():/storage/emulated/0/Android/data/com.zhangyu.myfilepath/files
2019-12-23 19:16:28.993 9584-9584/? E/MainActivity: PathUtils.getExternalAppCachePath():/storage/emulated/0/Android/data/com.zhangyu.myfilepath/cache
2019-12-23 19:16:28.996 9584-9584/? D/MainActivity: FileManager.get().getExternalStoragePath(): /storage/emulated/0
2019-12-23 19:16:29.000 9584-9584/? D/MainActivity: FileManager.get().getInternalAppFilesPath(): /data/user/0/com.zhangyu.myfilepath/file
2019-12-23 19:16:29.003 9584-9584/? D/MainActivity: FileManager.get().getInternalAppCachePath(): /data/user/0/com.zhangyu.myfilepath/cache
2019-12-23 19:16:29.007 9584-9584/? D/MainActivity: FileManager.get().getInternalAppSpPath(): /data/user/0/com.zhangyu.myfilepath/shared_prefs
2019-12-23 19:16:29.011 9584-9584/? D/MainActivity: FileManager.get().getExternalAppFilesPath(): /storage/emulated/0/Android/data/com.zhangyu.myfilepath/file
2019-12-23 19:16:29.014 9584-9584/? D/MainActivity: FileManager.get().getExternalAppCachePath(): /storage/emulated/0/Android/data/com.zhangyu.myfilepath/cache