Android实现读写SD卡
程序员文章站
2024-02-29 20:46:52
sd卡的读写是我们在开发android 应用程序过程中最常见的操作。下面介绍sd卡的读写操作方式:
1. 获取sd卡的根目录
复制代码 代码如...
sd卡的读写是我们在开发android 应用程序过程中最常见的操作。下面介绍sd卡的读写操作方式:
1. 获取sd卡的根目录
复制代码 代码如下:
string sdcardroot = environment.getexternalstoragedirectory().getabsolutepath();
2. 在sd卡上创建文件夹目录
/** * 在sd卡上创建目录 */ public file createdironsdcard(string dir) { file dirfile = new file(sdcardroot + file.separator + dir +file.separator); log.v("createdironsdcard", sdcardroot + file.separator + dir +file.separator); dirfile.mkdirs(); return dirfile; }
3. 在sd卡上创建文件
/** * 在sd卡上创建文件 */ public file createfileonsdcard(string filename, string dir) throws ioexception { file file = new file(sdcardroot + file.separator + dir + file.separator + filename); log.v("createfileonsdcard", sdcardroot + file.separator + dir + file.separator + filename); file.createnewfile(); return file; }
4.判断文件是否存在于sd卡的某个目录
/** * 判断sd卡上文件是否存在 */ public boolean isfileexist(string filename, string path) { file file = new file(sdcardroot + path + file.separator + filename); return file.exists(); }
5.将数据写入到sd卡指定目录文件
/* 写入数据到sd卡中 */ public file writedata2sdcard(string path, string filename, inputstream data) { file file = null; outputstream output = null; try { createdironsdcard(path); //创建目录 file = createfileonsdcard(filename, path); //创建文件 output = new fileoutputstream(file); byte buffer[] = new byte[2*1024]; //每次写2k数据 int temp; while((temp = data.read(buffer)) != -1 ) { output.write(buffer,0,temp); } output.flush(); } catch (exception e) { e.printstacktrace(); } finally{ try { output.close(); //关闭数据流操作 } catch (exception e2) { e2.printstacktrace(); } } return file; }
注意事项
对sd卡的操作,必须要申请权限:
<uses-permission android:name="android.permission.write_external_storage"/>
详情看这里
注意:不直接进行读出是为了防止打文件操作对内存的消耗,所以用输入流读到硬盘上。
public string readfile(string filename) throws exception{ fileinputstream fis = context.openfileinput(filename); byte[] bytes = new byte[fis.available()]; fis.read(bytes); fis.close(); return new string(bytes,"utf-8"); }
当文件很大的时候,byte[]会占用很大的内存。
package cn.itcast.fileio.service; import java.io.bytearrayoutputstream; import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import android.content.context; import android.os.environment; public class sdcardservice { private context ctx; public sdcardservice(context ctx) { this.ctx = ctx; } /** * 写文件入skcard */ public void writetosdcard(string filename, string cont) { try { // 判断是否有挂载sdcard if (environment.getexternalstoragestate().equals( environment.media_mounted)) { // 得到sdcar文件目录 file dir = environment.getexternalstoragedirectory(); file file = new file(dir, "itcast.txt"); fileoutputstream fos = new fileoutputstream(file); fos.write(cont.getbytes()); fos.close(); } } catch (exception e) { e.printstacktrace(); } } /** * 读sdcard中的文件 */ public string readsdcard(string filename) { try { // 判断是否有挂载sdcard if (environment.getexternalstoragestate().equals( environment.media_mounted)) { // 得到sdcar文件目录 file dir = environment.getexternalstoragedirectory(); file file = new file(dir, "itcast.txt"); fileinputstream fis = new fileinputstream(file); return readis2string(fis); } } catch (exception e) { e.printstacktrace(); } return null; } /** * 将输入流数据读取到输出流当中 */ private outputstream readis2os(inputstream is ,outputstream os){ try { byte[] bytes = new byte[1024]; int length = 0 ; while((length = is.read(bytes)) != -1){ os.write(bytes, 0, length); } is.close(); os.close(); } catch (ioexception e) { e.printstacktrace(); } return os ; } /** * 将输入流数据读取到输出流当中 */ public byte[] readis2bytes(inputstream is){ bytearrayoutputstream baos = new bytearrayoutputstream(); readis2os(is,baos); return baos.tobytearray() ; } public string readis2string(inputstream is){ try { return new string(readis2bytes(is),"utf-8"); } catch (exception e) { e.printstacktrace(); } return null ; } public string readis2string(string filename){ try { if(environment.getexternalstoragestate().equals(environment.media_mounted)){ file dir = environment.getexternalstoragedirectory(); file file = new file(dir,filename); fileinputstream is = new fileinputstream(file); return readis2string(is); } } catch (exception e) { e.printstacktrace(); } return null ; } }