sd卡 数据读取,显示在手机上
?
- ? ? ? ? ? ??/**?
- ?????????????*?判断sd卡是否存在?Environment.getExternalStorageState()?得到sd卡当前的状态?
- ? ? ? ? ? ??如果返回?MEDIA_MOUNTED表示外部存储设备存在。并且有读写的权限(因为sd卡有写保护?如果写保护关闭也是没有权限读写的)?
- ?????????????*/
?
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
//文件的路径和名字:
File file = new File(Environment.getExternalStorageDirectory(),fileNameString);
?
//建立一个基于这个文件的文件流。
FileInputStream fileInputStream = new FileInputStream(file);
?
//读取这个文件:
?String contentString = readDate(fileInputStream);
?
?public static ?String readDate (InputStream inputStream ) throws Exception
? ? {
? ? ? ? byte [] byte1 = new byte[1024];
? ? ? ? /**
? ? ? ? ?* 当输入流读到文件的末尾 返回就是-1?
? ? ? ? ?*/
? ? ? ? int length = inputStream.read(byte1);
? ? ? ? ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
? ? ? ? if(length!=-1)
? ? ? ? {
? ? ? ? ? ? //读到的内容存在内存中ByteArrayOutputStream 这个类用于将byte流存储在内存中
?
? ? ? ? ? ? byteArrayOutputStream.write(byte1, 0, length);
? ? ? ? }
? ? ? ? String dateString = ? byteArrayOutputStream.toString();
? ? ? ? byteArrayOutputStream.close();
? ? ? ? inputStream.close();
? ? ? ? return dateString;
? ? }
?
参考网址:http://leequer.iteye.com/blog/653022