Android 常用的路径、文件操作

Android中文件与文件夹的创建(file)

常用路径获取

在Android开发过程中,我们经常会对文件系统进行操作——存放、释放我们应用的数据。Android系统中提供了各种功能的文件目录,每个目录都有相应的特点和功能。

内部存储(Internal Storage)
(1)内部存储
内部存储是App的私有目录,当一个应用卸载之后,内部存储中的这些文件也被删除。Shared Preferences和SQLite数据库文件都是存储在内部存储空间上的。

//路径:(data/data/应用包名/files)
context.getFilesDir();
Log.e(TAG,getApplicationContext().getFilesDir().getAbsolutePath());

不过经实际测试(华为、小米手机等),getFileDir实际路径为: /data/user/0/ 应用包名/files

(2)缓存目录

//路径:(data/data/应用包名/cache),
context.getCacheDir();
Log.e(TAG,getApplicationContext().getCacheDir().getAbsolutePath());

应用程序的缓存目录,该目录内的文件在设备内存不足时会优先被删除掉,所以存放在这里的文件是没有任何保障的,可能会随时丢掉。
不过经实际测试(华为、小米手机等),getCacheDir的手机路径为: /data//data/user/0/应用包名/cache

外部存储(External Storage)
文件是可以被自由访问,且文件的数据对其他应用或者用户来说都是有意义的,当应用被卸载之后,其卸载前创建的文件仍然保留。
(1)公共文件(目录)

//SD卡的根目录,路径:/storage/emulated/0
Environment.getExternalStorageDirectory() 
Log.e(TAG, Environment.getExternalStorageDirectory().getAbsolutePath());

由于是外部存储的原因即使是这种类型的文件也能被其他程序访问,只不过一个应用私有的文件对其他应用其实是没有访问价值的(恶意程序除外)。
(2)私有文件(目录)
外部存储上,应用私有文件的价值在于卸载之后,这些文件也会被删除。类似于内部存储。

//路径:/storage/emulated/0/Android/data/应用包名/files
getApplicationContext().getExternalFilesDir("") ;
Log.e(TAG, getApplicationContext().getExternalFilesDir("").getAbsolutePath());

扩展: getExternalFilesDir是手机中设置 → 应用 → 具体应用详情→ 清除数据 的操作对象

//路径:/storage/emulated/0/Android/data/应用包名/cache
getApplicationContext().getExternalCacheDir() ;
Log.e(TAG,getApplicationContext().getExternalCacheDir().getAbsolutePath());

扩展: getExternalCacheDir是手机中设置 → 应用 → 具体应用详情→ 清除缓存的操作对象

文件相关操作

文件夹的创建和删除
文件的创建和删除
在demo中有简单的示例

demo

布局很简单就不贴出来了


Android 常用的路径、文件操作_第1张图片
image.png

相关代码

public class FileActivity extends BaseActivity {

    private TextView textView;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btn5;
    private Button btn6;
    private Button btn7;
    private Button btn8;
    private Button btn9;
    private Button btn10;

    @Override
    public int getLayoutId() {
        return R.layout.home_activity_file;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
        textView = (TextView) findViewById(R.id.textView);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        btn5 = (Button) findViewById(R.id.btn5);
        btn6 = (Button) findViewById(R.id.btn6);
        btn7 = (Button) findViewById(R.id.btn7);
        btn8 = (Button) findViewById(R.id.btn8);
        btn9 = (Button) findViewById(R.id.btn9);
        btn10 = (Button) findViewById(R.id.btn10);
    }

    @Override
    public void initListener() {
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        btn10.setOnClickListener(this);
    }

    @Override
    public void viewsClick(View view) {
        int id = view.getId();
        if (id == R.id.btn1) {
            //判断SD卡是否插入
            if (Environment.getExternalStorageState().equals(
                    android.os.Environment.MEDIA_MOUNTED)) {
                textView.setText("true");
            } else {
                textView.setText("false");
            }
        } else if (id == R.id.btn2) {
            //内部存储目录
            File file = getApplicationContext().getFilesDir();
            textView.setText(file.getPath());
        } else if (id == R.id.btn3) {
            //内部缓存目录
            File file = getApplicationContext().getCacheDir();
            textView.setText(file.getPath());
        } else if (id == R.id.btn4) {
            //SD卡的根目录
            File file = Environment.getExternalStorageDirectory();
            textView.setText(file.getPath());
        } else if (id == R.id.btn5) {
            //外部存储目录
            File file = getApplicationContext().getExternalFilesDir("");
            textView.setText(file.getPath());
        } else if (id == R.id.btn6) {
            //外部缓存目录
            File file = getApplicationContext().getExternalCacheDir();
            textView.setText(file.getPath());
        } else if (id == R.id.btn7) {
            //创建文件夹
            createFolder(getApplicationContext().getExternalFilesDir(""), "image");
        } else if (id == R.id.btn8) {
            //删除文件夹
            deleteFolder(getApplicationContext().getExternalFilesDir(""), "image");
        } else if (id == R.id.btn9) {
            //在image文件夹中创建文件,如果没有则创建文件夹并且创建文件
            createFile(getApplicationContext().getExternalFilesDir("image"), "text" + ".xml");
        } else if (id == R.id.btn10) {
            //删除文件
            deleteFile(getApplicationContext().getExternalFilesDir("image"), "text" + ".xml");
        }
    }

    /**
     * 创建文件夹
     *
     * @param path 路径
     * @param name 文件夹名
     */
    public void createFolder(File path, String name) {
        File Folder = new File(path + "/" + name);
        if (!Folder.exists())//判断文件夹是否存在,不存在则创建文件夹,已经存在则跳过
        {
            Folder.mkdir();//创建文件夹
        } else {
            Log.i("", "文件夹已存在");
        }
    }

    /**
     * 删除文件夹
     *
     * @param path 路径
     * @param name 文件夹名
     */
    public void deleteFolder(File path, String name) {
        File Folder = new File(path + "/" + name);
        if (Folder.exists())//判断文件夹是否存在
            Folder.delete();
    }

    /**
     * 创建文件
     *
     * @param path 路径
     * @param name 文件名,带格式
     */
    public void createFile(File path, String name) {
        //新建一个File类型的成员变量,传入文件名路径。
        File file = new File(path + "/" + name);
        //判断文件是否存在,存在就删除
        if (file.exists()) {
            file.delete();
        }
        try {
            //创建文件
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("creatXMLFileException", e.getMessage());
        }
    }

    /**
     * 删除文件
     *
     * @param path 路径
     * @param name 文件名,带格式
     */
    public void deleteFile(File path, String name) {
        File file = new File(path + "/" + name);
        //判断文件是否存在,存在就删除
        if (file.exists()) {
            file.delete();
        }
    }
}

不要在忘记在AndroidManifest.xml中添加权限

 
    
    
    
    
    

你可能感兴趣的:(Android 常用的路径、文件操作)