Package COM. yzxy. tool; import Java. io. bufferedinputstream; import Java. io. bufferedoutputstream; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. io. outputstream; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. OS. environment; Public Class fileutils {private string sdcardroot; private string sdstatestring; private final static string album_path = environment. getexternalstoragedirectory () + "/yueqiu/"; Public fileutils () {// get the directory sdcardroot = environment of the current external storage device. getexternalstoragedirectory (). getabsolutepath () + file. separator; // get the status of the extended SD card device sdstatestring = environment. getexternalstoragestate ();} public file createfileinsdca Rd (string Dir, string filename) throws ioexception {file = new file (sdcardroot + dir + file. separator + filename); file. createnewfile (); Return file;} public file creatsddir (string DIR) {file dirfile = new file (sdcardroot + dir + file. separator); If (! Dirfile. exists () {dirfile. mkdirs () ;}return dirfile;} public Boolean isfileexist (string filename) {file = new file (album_path + filename); Return file. exists ();} Public String getfilepath (string Dir, string filename) {return sdcardroot + dir + file. separator + filename;} public long getsdavailablesize () {If (sdstatestring. equals (Android. OS. environment. media_mounted) {// get the sdcard file path file pathfile = Droid. OS. environment. getexternalstoragedirectory (); android. OS. statfs = new android. OS. statfs (pathfile. getpath (); // obtain the sizelong nblocsize = statfs. getblocksize (); // obtain the number of blocks available for the program. Long navailablock = statfs. getavailableblocks (); // calculate the remaining sdcard size bytelong nsdfreesize = navailablock * nblocsize; return nsdfreesize;} return 0;} public Boolean write2sd (string Dir, string filename, Te [] bytes) {If (Bytes = NULL) {return false;} outputstream output = NULL; try {// has read and write permissions, and there is enough capacity if (sdstatestring. equals (Android. OS. environment. media_mounted) & bytes. length <getsdavailablesize () {file = NULL; creatsddir (DIR); file = createfileinsdcard (Dir, filename); Output = new bufferedoutputstream (New fileoutputstream (File); output. write (bytes); output. flush (); Return true ;}} catch (ioexce Ption E1) {// todo auto-generated catch blocke1.printstacktrace ();} finally {try {If (output! = NULL) {output. close () ;}} catch (exception e) {e. printstacktrace () ;}} return false;} public bitmap readbitmap (string path) {file = new file (album_path + path); bitmapfactory. options opt = new bitmapfactory. options (); If (! File. exists () {return NULL;} inputstream = NULL; try {inputstream = new bufferedinputstream (New fileinputstream (File); OPT. inpreferredconfig = bitmap. config. rgb_565; OPT. inpurgeable = true; OPT. ininputexcepable = true;} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} // bitmap bit = bitmapfactory. decodefile (album_path + path );/ /Return bit; return bitmapfactory. decodestream (inputstream, null, OPT);} public byte [] readfromsd (string Dir, string filename) {file = new file (sdcardroot + dir + file. separator + filename); If (! File. exists () {return NULL;} inputstream = NULL; try {inputstream = new bufferedinputstream (New fileinputstream (File); byte [] DATA = new byte [inputstream. available ()]; inputstream. read (data); return data;} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} finally {try {If (inputstream! = NULL) {inputstream. close () ;}} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace () ;}return NULL;} public file write2sdfrominput (string Dir, string filename, inputstream input) {file = NULL; outputstream output = NULL; try {int size = input. available (); // has the read and write permissions, and has sufficient capacity if (sdstatestring. equals (Android. OS. environment. media_mounted) & size <getsdavailablesize ()) {Creatsddir (DIR); file = createfileinsdcard (Dir, filename); Output = new bufferedoutputstream (New fileoutputstream (File); byte buffer [] = new byte [4*1024]; int temp; while (temp = input. read (buffer ))! =-1) {output. write (buffer, 0, temp);} output. flush () ;}} catch (ioexception E1) {// todo auto-generated catch blocke1.printstacktrace () ;} finally {try {If (output! = NULL) {output. close () ;}} catch (exception e) {e. printstacktrace () ;}} return file;} public void SaveFile (Bitmap BM, string filename) throws ioexception {file dirfile = new file (album_path); filename = filename. replace ("/", "_"); If (! Dirfile. exists () {dirfile. mkdir ();} file mycapturefile = new file (album_path + filename); bufferedoutputstream Bos = new bufferedoutputstream (New fileoutputstream (mycapturefile); BM. compress (bitmap. compressformat. JPEG, 80, Bos); Bos. flush (); Bos. close (); system. out. println ("saved successfully ");}}