Android--手机外部SD卡存储

首先需要一个如图所示的布局

Android--手机外部SD卡存储_第1张图片

这样的布局好些,就不多说了

主要涉及的知识点

数据保存的路径

路径1:/storage/sdcard/Android/data/packageName/Files

路径2:/storage/sdcard/xxx/

路径1:其它应用可以访问,应用程序卸载时删除

路径2:其它应用可以访问,应用程序卸载时不会删除

必须保证sd卡必须挂载

相关API

Environment:操作SD卡的工具类

得到sd卡的状态:Environment.getExternalStorageState()

得到sd卡的路径:Environment.getExternalStorageDirectory()

sd卡可读写的挂载状态 Environment.MEDIA_MOUNTED

context.getExternalFilesDir(null).getAbsolutePath()得到绝对路径

操作sd卡的权限android.permission.WRITE_EXTERNAL_STORAGE

主要分为两部分

一.路径1的保存和读取

先保存
public void sure1(View v) throws Exception {
        //判断sd卡状态,如果是挂载状态才保存,否则将进行提示
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            //读取文件的内容以及文件名
            String name = et_name.getText().toString();
            String content = et_content.getText().toString();
            //得到制定的outputStream
                //得到sd下的files路径
                String filesPath = getExternalFilesDir(null).getAbsolutePath();
                //组成完整的路径
                String path = filesPath+"/"+name;
            //创建FileOutputSream
            FileOutputStream fos = new FileOutputStream(path);
            //写数据
            fos.write(content.getBytes("utf-8"));
            fos.close();
            //提示
            Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this,"请插入SD卡",Toast.LENGTH_SHORT).show();
        }
    }

读取
 public void read1(View v) throws Exception {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            //得到路径
            String name = et_name.getText().toString();
            String filePath = getExternalFilesDir(null).getAbsolutePath();
            String path = filePath+"/"+name;
            //创建FileInputStream
            FileInputStream fis = new FileInputStream(path);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte [] buffer = new byte[1024];
            int len=-1;
            while ((len=fis.read(buffer))!=-1) {
                baos.write(buffer,0,len);
            }
            String content = baos.toString();
            et_content.setText(content);
        } else {
            Toast.makeText(this,"请插入SD卡",Toast.LENGTH_SHORT).show();
        }
    }

二路径2的保存和读取

保存
public void sure2(View v) throws Exception {
        //判断sd卡是否挂载
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            //读取输入的文件名以及文件内容
            String content= et_content.getText().toString();
            String name = et_name.getText().toString();
            //得到指定文件的OutputStream
           // 1)/storage/sdcard
            String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
            // 2)/storage/sdcard/fanny/(没有的话就创建)
            File file = new File(sdPath+"/fanny");
            if (!file.exists()) {
                file.mkdirs();//创建文件夹
            }
            //3)/storage/sdcard/fanny/Xxx.txt
            String path = sdPath+"/fanny"+name;
            //创建输出流
            FileOutputStream fos = new FileOutputStream(path);
            //写数据
            fos.write(content.getBytes("utf-8"));
            fos.close();
            Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this,"请插入sd卡",Toast.LENGTH_SHORT).show();
        }
    }


读取

public void read2(View V) throws Exception {
        //判断sd卡是否挂载
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            //得到输入的文件名
            String name = et_name.getText().toString();
            String filePath = Environment.getExternalStorageDirectory().getAbsolutePath();
            String path = filePath+"/fanny"+name;
            //得到指定文件的FileInputStream
            FileInputStream fis = new FileInputStream(path);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte [] buffer = new byte[2014];
            int len = -1;
            while ((len=fis.read(buffer))!=-1) {
                baos.write(buffer,0,len);
            }
           String content = baos.toString();
            et_content.setText(content);
        } else {
            Toast.makeText(this,"请插入SD卡",Toast.LENGTH_LONG).show();
        }
    }


你可能感兴趣的:(android)