- /**
- * 判断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;
}
本文介绍了一种在Android环境中检查SD卡是否可用的方法,并提供了如何从SD卡上的指定文件中读取内容的具体实现代码。
1787

被折叠的 条评论
为什么被折叠?



