读写sdk文件

本文介绍了在Android中进行SDK文件读写所需的权限设置,包括`MOUNT_UNMOUNT_FILESYSTEMS`和`WRITE_EXTERNAL_STORAGE`。通过`Environment.getExternalStorageState()`检查SD卡状态,并使用`Environment.getExternalStorageDirectory()`获取外部存储目录。详细步骤涵盖了判断SD卡存在、获取读写权限、使用IO流操作文件以及在AndroidManifest.xml中添加相应权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

添加相关权限

  1. <!-- 在SDCard中创建与删除文件权限 -->  
  2.   
  3. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />  
  4.   
  5. <!-- 往SDCard中写入数据权限 -->  
  6.   
  7. <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中添加权限





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值