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