文件、文件路径(path)详解及文件夹下所有文件获取方法介绍

转载请注明出处:文件、文件路径(path)详解及文件夹下所有文件获取方法介绍_path文件解析_Mr_Leixiansheng的博客-CSDN博客

关于Android中几个常用文件路径的介绍

我记忆力不是太好,总是记不清,那几个常用的文件路径的具体名字,所以就在此记录一下,也方便不太清楚的同学查阅。

外部存储,内部存储的区别

Internal storage: 

  • 总是可用的 
  • 这里的文件默认只能被我们的app所访问。 
  • 当用户卸载app的时候,系统会把internal内该app相关的文件都清除干净。 
  • Internal是我们在想确保不被用户与其他app所访问的最佳存储区域。 


External storage: 

  • 并不总是可用的,因为用户有时会通过USB存储模式挂载外部存储器,当取下挂载的这部分后,就无法对其进行访问了。 
  • 是大家都可以访问的,因此保存在这里的文件可能被其他程序访问 
  • 当用户卸载您的应用时,只有在您通过 getExternalFilesDir() 将您的应用的文件保存在目录中时,系统才会从此处删除您的应用的文件 
  • External是在不需要严格的访问权限并且希望这些文件能够被其他app所共享或者是允许用户通过电脑访问时的最佳存储区域。 

常见的获取文件路径方法,对应的绝对路径

getCacheDir().getAbsolutePath():

/data/data/packagename/cache

getFilesDir().getAbsolutePath():

/data/data/packagename/files

以上为内存存储路径,以下为外部存储路径

getExternalCacheDir().getAbsolutePath():
可以作为外部缓存的路径,卸载app时,会自动删除文件 
/storage/emulated/0/Android/data/packagename/cache

Environment.getExternalStorageDirectory().getAbsolutePath():

/storage/emulated/0

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath()):

/storage/emulated/0/DCIM

getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath:
 

/storage/emulated/0/Android/data/packagename/files/Pictures

获取文件夹下所有文件:

File[] files = new File(path).listFiles();

示例代码如下:

package com.leixiansheng.fileandpath;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import java.io.File;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private TextView fileAbsolutePath;
    private String path = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fileAbsolutePath = findViewById(R.id.file_path);

        String cacheDir = getCacheDir().getAbsolutePath();
        String filesDir = getFilesDir().getAbsolutePath();
        String externalFilesDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath();
        String externalCacheDir = getExternalCacheDir().getAbsolutePath();
        String externalStorageDirectory = Environment.getExternalStorageDirectory().getAbsolutePath();
        String externalStoragePublicDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
        String externalStorageState = Environment.getExternalStorageState();

        path = externalStorageDirectory + File.separator + "Test" + File.separator;

        fileAbsolutePath.setText("cacheDir:" + cacheDir + "\n" + "\n"
                + "filesDir:" + filesDir + "\n" + "\n"
                + "externalFilesDir:" + externalFilesDir + "\n" + "\n"
                + "externalCacheDir:" + externalCacheDir + "\n" + "\n"
                + "externalStorageDirectory:" + externalStorageDirectory + "\n" + "\n"
                + "externalStoragePublicDirectory:" + externalStoragePublicDirectory + "\n" + "\n"
                + "externalStorageState:" + externalStorageState + "\n" + "\n"
                + "path:" + path);

        getFileList(path);

        Log.i(TAG, "cacheDir:" + cacheDir + "\n"
                + "filesDir:" + filesDir + "\n"
                + "externalFilesDir:" + externalFilesDir + "\n"
                + "externalCacheDir:" + externalCacheDir + "\n"
                + "externalStorageDirectory:" + externalStorageDirectory + "\n"
                + "externalStoragePublicDirectory:" + externalStoragePublicDirectory + "\n"
                + "externalStorageState:" + externalStorageState + "\n"
                + "path:" + path);

    }

    //获取文件夹下的所有文件名
    public void getFileList(String path) {
        File[] files = new File(path).listFiles();

        if (files == null) {
            return;
        }
        for (int i = 0; i < files.length; i++) {
            Log.i(TAG, files[i].getName());
        }
    }
}

注意:获取外部文件记得给权限!

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.leixiansheng.fileandpath">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

打印Log 

01-29 11:13:46.945 16689-16689/com.leixiansheng.fileandpath I/MainActivity: test3.txt
01-29 11:13:46.945 16689-16689/com.leixiansheng.fileandpath I/MainActivity: test1.txt
01-29 11:13:46.945 16689-16689/com.leixiansheng.fileandpath I/MainActivity: test5.txt
01-29 11:13:46.945 16689-16689/com.leixiansheng.fileandpath I/MainActivity: test2.txt
01-29 11:13:46.945 16689-16689/com.leixiansheng.fileandpath I/MainActivity: test4.txt
01-29 11:13:46.945 16689-16689/com.leixiansheng.fileandpath I/MainActivity: test6.txt
01-29 11:13:46.946 16689-16689/com.leixiansheng.fileandpath I/MainActivity: cacheDir:/data/data/com.leixiansheng.fileandpath/cache
                                                                            filesDir:/data/data/com.leixiansheng.fileandpath/files
                                                                            externalFilesDir:/storage/emulated/0/Android/data/com.leixiansheng.fileandpath/files/Pictures
                                                                            externalCacheDir:/storage/emulated/0/Android/data/com.leixiansheng.fileandpath/cache
                                                                            externalStorageDirectory:/storage/emulated/0
                                                                            externalStoragePublicDirectory:/storage/emulated/0/DCIM
                                                                            externalStorageState:mounted
                                                                            path:/storage/emulated/0/Test/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值