Android 获取,内置目录,外置目录的路径,path

获取内置目录,外置目录的路径

方案一:第三方框架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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值