在外部存储(SD卡)读写文件及检测SD卡状态详解

来源:互联网 发布:广告优化师 编辑:程序博客网 时间:2024/06/05 15:46

1、外部存储一般都是手机的SD卡,SD卡相当于电脑的电脑硬盘;

2、SD卡路径

   2.2之前  sdcard

   4.3之前  mnt/sdcard

   4.3开始   storage--sdcard

所以我们在SD卡读写任何数据的时候可以这么写:

File file=new File(“sdcard”)

File file=new File(“mnt/sdcard”)

File file=new File(“storage/sdcard“)

3、获取标准Sd卡路径

Environment.getExternalStorageDirectory()  ;

由于许多厂商将路径都改了,所以我们在使用SD卡读取文件的时候一定要借助API   Environment.getExternalStorageDirectory()获取Sd卡存储路径

File file1=new File(Environment.getExternalStorageDirectory(),"info1");
4、在SD卡内部写文件是需要权限的,在此列出来
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
我的在虚拟机上运行不出来,不晓得什么原因,还没找到,此处待更新...!
5、SD卡读写有权限,但是在实际操作SD卡的时候,必须要先检测SD卡的状态,在这里有几种常见的状态需要牢记:
MEDIA_BAD_REMOVAL     Storage state if the media was removed before it was unmounted. SD卡不存在
MEDIA_CHECKING    Storage state if the media is present and being disk-checked.SD卡正在检测
MEDIA_MOUNTED     Storage state if the media is present and mounted at its mount point with read/write access.SD卡已经挂在,可用
MEDIA_UNKNOWN     Storage state if the media is present and being disk-checked.SD卡无法识别
MEDIA_UNMOUNTED   Storage state if the media is present but not mounted.SD卡存在但没有挂在
      
public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        try {            readAcount();        } catch (FileNotFoundException e) {            e.printStackTrace();        }    }    //创建一个方法读取文件    public void readAcount() throws FileNotFoundException {           //文件路径一致   data/data/packagename/textname       // File file=new File(getFilesDir(),"info.txt");
             //检测SD卡状态,看是否可用        if(Environment.getExternalStorageState()==Environment.MEDIA_MOUNTED){            File file=new File(Environment.getExternalStorageDirectory(),"info1.txt");            //            //File file =new File("data/data/writeinrom.workharder.com.writeinrom/info.txt");            EditText ed_name = (EditText) findViewById(R.id.ed_name);            EditText ed_pass = (EditText) findViewById(R.id.ed_password);            //定义输入流            try{                //定义一个输入流                FileInputStream fileInputStream=new FileInputStream(file);                //将字节输入流转化为字符输入流                BufferedReader bufferedInputStream = new BufferedReader(new InputStreamReader(fileInputStream));                //读取用户输入的用户名和密码                String text=bufferedInputStream.readLine();                //标准分隔符,使用分隔符拆分用户名和密码                String[] strings=text.split("##");                //数据回显                ed_name.setText(strings[0]);                ed_pass.setText(strings[1]);            }catch(Exception e){                e.printStackTrace();            }        }else{            Toast.makeText(this,"SD卡不能用哦,么么哒",Toast.LENGTH_SHORT).show();        }           }    public void sure(View view) throws IOException {        if(Environment.getExternalStorageState()==Environment.MEDIA_MOUNTED){            File file=new File(Environment.getExternalStorageDirectory(),"info1.txt");            //File file =new File("data/data/writeinrom.workharder.com.writeinrom/info.txt");            EditText ed_name = (EditText) findViewById(R.id.ed_name);            EditText ed_pass = (EditText) findViewById(R.id.ed_password);            String name = ed_name.getText().toString();            String passWord = ed_pass.getText().toString();            //CheckBox 是勾选框,简单的View组件,只要判断是否被勾选即可            CheckBox cb = (CheckBox) findViewById(R.id.cb);            if (cb.isChecked()) {                //data/data/writeinrom.workharder.com.writeinrom 为内部存储空间的标准读写路径                FileOutputStream outputStream=new FileOutputStream(file);                try{                    //写文件内部形参是byte【】                    outputStream.write((name + "##" + passWord).getBytes());                    Log.i("Tag","success");                    outputStream.close();                }catch (Exception e){                    e.printStackTrace();                }                System.out.print("登录成功");                Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();            }        }        }}





0 0
原创粉丝点击