添加相关权限
- <!-- 在SDCard中创建与删除文件权限 -->
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
- <!-- 往SDCard中写入数据权限 -->
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
要判断一下是否插入SD卡和是否具有读写的能力:
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
详细代码如下
/**
*
* @param fileName
* 文件的名字
* @param content
* 文件的内容
* @return
*/
public boolean saveContentToSdCard(String fileName, String content) {
boolean flag = false;
FileOutputStream fileOutputStream = null;
// sdcard所在的路径
File file = new File(Environment.getExternalStorageDirectory(), fileName);
//
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
try {
// true 写入的内容可追加
fileOutputStream = new FileOutputStream(file, true);
fileOutputStream.write(content.getBytes());
fileOutputStream.write("\r\n".getBytes());
flag = true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return flag;
}写:
public String getFileFromSdcard(String fileName) {
FileInputStream inputstream = null;
// 缓冲区缓存的流,和磁盘无关,不需要关闭
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
File file = new File(Environment.getExternalStorageDirectory(), fileName);
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
try {
inputstream = new FileInputStream(file);
int len = 0;
byte[] data = new byte[1024];
while ((len = inputstream.read(data)) != -1) {
outputStream.write(data, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (inputstream != null) {
try {
inputstream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return new String(outputStream.toByteArray());
}Environmet类参数介绍
常用常量
|
String MEDIA_MOUNTED |
当前Android的外部存储器可读可写 |
|
String MEDIA_MOUNTED_READ_ONLY |
当前Android的外部存储器只读 |
常用方法
|
方法名称 |
描述 |
|
public static File getDataDirectory () |
获得Android下的data文件夹的目录 |
|
public static File getDownloadCacheDirectory () |
获得Android Download/Cache 内容的目录 |
|
public static File getExternalStorageDirectory () |
获得Android外部存储器也就是SDCard的目录 |
|
public static String getExternalStorageState () |
获得Android外部存储器的当前状态 |
|
public static File getRootDirectory () |
获得Android下的root文件夹的目录 |
总结几个步骤:
1、 先判断这台手机设备上是否有SDCard且具有读写SDCard的权限
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
2、 调用Environment.getExternalStorageDirectory()获得到外部存储器的目录
3、 使用IO流对外部存储器进行文件的读写
4、 在AndroidMainfest.xml中添加权限
本文介绍了在Android中进行SDK文件读写所需的权限设置,包括`MOUNT_UNMOUNT_FILESYSTEMS`和`WRITE_EXTERNAL_STORAGE`。通过`Environment.getExternalStorageState()`检查SD卡状态,并使用`Environment.getExternalStorageDirectory()`获取外部存储目录。详细步骤涵盖了判断SD卡存在、获取读写权限、使用IO流操作文件以及在AndroidManifest.xml中添加相应权限。
1234

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



