Android获取系统文件常用路径(详细)

在android 6.0以前,你可以只关注外置存储是否挂载即可,但是从6.0以后,也就是M系统后,还需要判断是否有读写权限,只有具备这些权限才可以读写外置存储。

package com.jinfeng.gongshigonggao.utils;

import android.content.Context;
import android.os.Environment;

public class FilePathUtil {

    /**
     * 获取路径:/data/user/0/应用包名/files
     * 该目录是应用的文件存储目录,应用被卸载时,该目录一同被系统删除。
     * 不会因为系统内存不足而被清空。
     * 默认存在,默认具备读写权限(6.0系统可以不用向用户申请)
     * @param context
     * @return
     */
    public static String getFileDir(Context context){
        return context.getFilesDir().getAbsolutePath();
    }

    /**
     * 获取路径:/data/user/0/应用包名/cache
     * 该目录是应用的文件缓存目录,应用被卸载时,该目录一同被系统删除。
     * 默认存在,默认具备读写权限。不同于getFileDir,该目录下的文件在系统内存紧张时,会被清空文件,来腾出空间供系统使用。
     * 著名的图片加载库ImageLoader就是在没有外置存储读写权限时使用此文件夹。
     * getFileDir,不会因为系统内存不足而被清空。(6.0系统可以不用向用户申请)
     * @param context
     * @return
     */
    public static String getCacheDir(Context context){
        return context.getCacheDir().getAbsolutePath();
    }

    /**
     * 获取路径:/storage/emulated/0/Android/obb/应用包名
     * 该目录是应用的数据存放目录,一般被用来存放游戏数据包obb文件。
     * 默认存在,可读写(6.0系统可以不用向用户申请)
     * @param context
     * @return
     */
    public static String getObbDir(Context context){
        return context.getObbDir().getAbsolutePath();
    }

    /**
     * 获取路径:/data/user/0/应用包名/code_cache
     * 默认存在,可读写。(6.0系统可以不用向用户申请)
     *
     * @param context
     * @return
     */
    public static String getCodeCacheDir(Context context){
        return context.getCodeCacheDir().getAbsolutePath();
    }

    /**
     * 获取路径:(以下载目录为准) /storage/emulated/0/Android/data/应用包名/files/Download
     * 默认存在,可读写。(6.0系统可以不用向用户申请)
     * @param context
     * @param s
     * @return
     */
    public static String getExternalFilesDir(Context context, String s){
        return context.getExternalFilesDir(s).getAbsolutePath();
    }


    /**
     * 获取路径:/storage/emulated/0/Android/data/应用包名/cache
     * 默认存在,可读写。(6.0系统可以不用向用户申请)
     * @param context
     * @return
     */
    public static String getExternalCacheDir(Context context){
        return context.getExternalCacheDir().getAbsolutePath();
    }

    /**
     * 获取路径:/data/user/0/应用包名/databases/参数名
     * 默认不存在,可读写。(6.0系统可以不用向用户申请)
     * @param context
     * @param s
     * @return
     */
    public static String getDatabasePath(Context context, String s){
        return context.getDatabasePath(s).getAbsolutePath();
    }

    /**
     * 获取路径:/data/user/0/应用包名/app_参数名
     * 默认存在,可读写。分为Private等三个权限,private代表仅能自己访问。(6.0系统可以不用向用户申请)
     * @param context
     * @param s
     * @param i Context.MODE_PRIVATE
     * @return
     */
    public static String getDir(Context context, String s, int i){
        return context.getDir(s, i).getAbsolutePath();
    }

    /**
     * 获取路径:/data/app/应用包名-1/base.apk
     * 默认存在,获取apk包路径
     * @param context
     * @return
     */
    public static String getPackageCodePath(Context context){
        return context.getPackageCodePath();
    }

    /**
     * 获取路径:/storage/emulated/0
     * 默认存在,声明权限则可读写(6.0和以后系统还需要向用户申请同意才可以)
     * @return
     */
    public static String getExternalStorageDirectory(){
        return Environment.getExternalStorageDirectory().getAbsolutePath();
    }

    /**
     * 获取路径:/storage/emulated/0/Download(以下载目录为例)
     * 默认存在,声明权限则可读写(6.0和以后系统还需要向用户申请同意才可以)
     * @param s
     * @return
     */
    public static String getExternalStoragePublicDirectory(String s){
        return Environment.getExternalStoragePublicDirectory(s).getAbsolutePath();
    }

    /**
     * 获取路径:/data/cache
     * 默认存在,声明权限则可读写(6.0和以后系统还需要向用户申请同意才可以)
     * @return
     */
    public static String getDownloadCacheDirectory(){
        return Environment.getDownloadCacheDirectory().getAbsolutePath();
    }

    /**
     * 获取路径:/data/user/应用包名/files/download(示例download)
     * 该目录是应用的文件存储目录,应用被卸载时,该目录一同被系统删除。
     * 默认存在,默认具备读写权限(6.0系统可以不用向用户申请)
     * @param context
     * @param s
     * @return
     */
    public static String getFileStreamPath(Context context, String s){
        return context.getFileStreamPath(s).getAbsolutePath();
    }
}

代码中调用

private void checkFilePath(){
        LogUtil.e(TAG, "getFileDir : " + FilePathUtil.getFileDir(this));
        LogUtil.e(TAG, "getCacheDir : " + FilePathUtil.getCacheDir(this));
        LogUtil.e(TAG, "getObbDir : " + FilePathUtil.getObbDir(this));
        LogUtil.e(TAG, "getCodeCacheDir : " + FilePathUtil.getCodeCacheDir(this));
        LogUtil.e(TAG, "getExternalFilesDir : " + FilePathUtil.getExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS));
        LogUtil.e(TAG, "getExternalCacheDir : " + FilePathUtil.getExternalCacheDir(this));
        LogUtil.e(TAG, "getDatabasePath : " + FilePathUtil.getDatabasePath(this, "show.db"));
        LogUtil.e(TAG, "getDir : " + FilePathUtil.getDir(this, "show", MODE_PRIVATE));
        LogUtil.e(TAG, "getPackageCodePath : " + FilePathUtil.getPackageCodePath(this));
        LogUtil.e(TAG, "getExternalStorageDirectory : " + FilePathUtil.getExternalStorageDirectory());
        LogUtil.e(TAG, "getExternalStoragePublicDirectory : " + FilePathUtil.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
        LogUtil.e(TAG, "getDownloadCacheDirectory : " + FilePathUtil.getDownloadCacheDirectory());
        LogUtil.e(TAG, "getFileStreamPath : " + FilePathUtil.getFileStreamPath(this, Environment.DIRECTORY_DOWNLOADS));
    }

参考于: https://blog.csdn.net/nicepainkiller/article/details/81181861

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android系统下载管理DownloadManager增强方法,可用于包括获取下载相关信息,如: getStatusById(long) 得到下载状态 getDownloadBytes(long) 得到下载进度信息 getBytesAndStatus(long) 得到下载进度信息和状态 getFileName(long) 得到下载文件路径 getUri(long) 得到下载uri getReason(long) 得到下载失败或暂停原因 getPausedReason(long) 得到下载暂停原因 getErrorCode(long) 得到下载错误码 =================================================================== package cn.trinea.android.common.util; import java.lang.reflect.Method; import android.app.DownloadManager; import android.app.DownloadManager.Request; import android.database.Cursor; import android.net.Uri; import android.os.Build; /** * DownloadManagerPro * * Get download info * {@link #getStatusById(long)} get download status * {@link #getDownloadBytes(long)} get downloaded byte, total byte * {@link #getBytesAndStatus(long)} get downloaded byte, total byte and download status * {@link #getFileName(long)} get download file name * {@link #getUri(long)} get download uri * {@link #getReason(long)} get failed code or paused reason * {@link #getPausedReason(long)} get paused reason * {@link #getErrorCode(long)} get failed error code * * * Operate download * {@link #isExistPauseAndResumeMethod()} whether exist pauseDownload and resumeDownload method in * {@link DownloadManager} * {@link #pauseDownload(long...)} pause download. need pauseDownload(long...) method in {@link DownloadManager} * {@link #resumeDownload(long...)} resume download. need resumeDownload(long...) method in {@link DownloadManager} * * * RequestPro * {@link RequestPro#setNotiClass(String)} set noti class * {@link RequestPro#setNotiExtras(String)} set noti extras * * * @author Trinea 2013-5-4 */ public class DownloadManagerPro { public static final Uri CONTENT_URI
Android 开发中,获取文件路径的方法有很多种。其中,常用的方法有以下几种: 1. 通过 Uri 获取路径 ```java public String getRealPathFromUri(Uri uri) { String[] projection = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver().query(uri, projection, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String filePath = cursor.getString(column_index); cursor.close(); return filePath; } ``` 2. 通过 FileProvider 获取路径 ```java public static Uri getUriFromFile(Context context, File file) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { return FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", file); } else { return Uri.fromFile(file); } } public String getRealPathFromUri(Uri uri) { String filePath = ""; if (DocumentsContract.isDocumentUri(this, uri)) { String documentId = DocumentsContract.getDocumentId(uri); if ("com.android.providers.media.documents".equals(uri.getAuthority())) { String id = documentId.split(":")[1]; String selection = MediaStore.Images.Media._ID + "=" + id; filePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection); } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) { Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(documentId)); filePath = getImagePath(contentUri, null); } } else if ("content".equalsIgnoreCase(uri.getScheme())) { filePath = getImagePath(uri, null); } else if ("file".equalsIgnoreCase(uri.getScheme())) { filePath = uri.getPath(); } return filePath; } public String getImagePath(Uri uri, String selection) { String path = null; Cursor cursor = getContentResolver().query(uri, null, selection, null, null); if (cursor != null) { if (cursor.moveToFirst()) { path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); } cursor.close(); } return path; } ``` 3. 直接获取文件路径 ```java File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.txt"); String filePath = file.getAbsolutePath(); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值