Home » Java » Android development SDCardUtils tool class.Java to...

Android development SDCardUtils tool class.Java tools detailed code, with the source code

package com.xiaobing.zhbj.utils;
Import java.io.BufferedInputStream;
Import java.io.BufferedOutputStream;
Import java.io.ByteArrayOutputStream;
Import java.io.File;
Import java.io.FileInputStream;
Import java.io.FileOutputStream;
Import java.io.IOException;
Import android.os.Environment;
/ * *
*
*
* @author:QQ:986945193
*
* @ Sina micro-blog: http://weibo.com/mcxiaobing
*
* @version V1.0 official edition
*
* SDCard utility class
*
* /
Public, class, SDCardUtils {
/ * *
* to determine whether the SD card is mounted
*
* @return
* /
Public, static, Boolean, isMounted () {
Return Environment.getExternalStorageState ().Equals
Environment.MEDIA_MOUNTED);
}
Get / / SD card root path
Public, static, String, getSDPath () {
If (isMounted ()) {
Return Environment.getExternalStorageDirectory ().GetAbsolutePath ();
}
Return null;
}
/ / save the file in the SD card
Public, static, Boolean, saveFileIntoSDCard (byte[], data, String, path),
String, fileName) {
If (isMounted ()) {
BufferedOutputStream BOS = null;
{try
String filePath = getSDPath () + File.separator + path;
File file = new File (filePath);
If (... File.exists ()) {
File.mkdirs ();
}
BOS = new, BufferedOutputStream (new, FileOutputStream (new, File)
File, fileName));
Bos.write (data, 0, data.length);
Bos.flush ();
Return true;
} catch (Exception, e) {
E.printStackTrace ();
} finally {
If (Bos = = null) {
{try
Bos.close ();
} catch (IOException, E1) {
E1.printStackTrace ();
}
}
}
}
Return false;
}
Remove the memory / from the SD card file
Public, static, byte[], getFileFromSDCard (String, filePath) {
If (isMounted ()) {
File file = new File (filePath);
BufferedInputStream bis = null;
ByteArrayOutputStream baos = null;
If (file.exists ()) {
{try
Baos = new, ByteArrayOutputStream ();
BIS = new, BufferedInputStream (new, FileInputStream (file));
Int len = 0;
Byte[] buffer = new byte[1024 * 8];
While ((= len = bis.read (buffer)) = = -1) {
Baos.write (buffer, 0, len);
Baos.flush ();
}
Return, baos.toByteArray ();
} catch (Exception, e) {
E.printStackTrace ();
} finally {
If (BIS = = null) {
{try
Bis.close ();
Baos.close ();
} catch (IOException, e) {
E.printStackTrace ();
}
}
}
}
}
Return null;
}
}

Latest