万能存储工具类 SDCard存储 /data/data/存储 assets存储
raw存储
粘贴过去就可以用了
<uses-permission android:name="android.permission.INTERNET" />
<!-- SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 向SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
package com.hexun.util;
import java.io.ByteArrayOutputStream;
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 java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.util.EncodingUtils;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
/**
* 保存图片的类
*
*
*/
public class FileUtil {
private final static String CACHE = "/image";
private final static String CHARSETNAME = "UTF-8";
/*************************** 从resource的raw中读取文件数据 *****************************************************/
/**
* raw中读取文件数据
*
* @param context
* @param fileName
* R.raw.test
* @return String 文件内容
*/
public static String readRaw(Context context, int fileName) {
try {
// 得到资源中的Raw数据流
InputStream in = context.getResources().openRawResource(fileName);
return readInToStr(in);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*************************** 从resource的asset中读取文件数据 **************************************************/
/**
* asset中读取文件数据
*
* @param context
* @param fileName
* 文件名
* @return String 文件内容
*/
public static String readAssets(Context context, String fileName) {
try {
if (isNullEmptyBlank(fileName)) {
return null;
}
// 得到资源中的asset数据流
InputStream in = context.getResources().getAssets().open(fileName);
return readInToStr(in);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/***************************** data/data/ *****************************************************************/
// MODE_PRIVATE 私有(只能创建它的应用访问) 重复写入时会文件覆盖
// MODE_APPEND 私有 重复写入时会在文件的末尾进行追加,而不是覆盖掉原来的文件
// MODE_WORLD_READABLE 公用 可读
// MODE_WORLD_WRITEABLE 公用 可读写
/**
* 写数据/data/data/
*
* @param context
* @param fileName
* 文件名
* @param str
* 数据内容
* @return 成功 false
*/
public static boolean saveDataStr(Context context, String fileName,
String writeStr) {
if (isNullEmptyBlank(fileName) || isNullEmptyBlank(writeStr)) {
try {
String path = context.getFilesDir().getAbsolutePath();
File file = new File(path, fileName);
if (file.exists()) {
file.delete();
}
OutputStream os = new FileOutputStream(file);
return saveOuToStr(writeStr, os);
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}
/**
* 读数据/data/data/
*
* @param context
* @param fileName
* 文件名
* @return String 文件内容
*/
public static String readDataStr(Context context, String fileName) {
if (isNullEmptyBlank(fileName)) {
try {
String path = context.getFilesDir().getAbsolutePath();
File file = new File(path, fileName);
if (file.exists()) {
InputStream in = new FileInputStream(file);
return readInToStr(in);
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/***************************** /data/data/图片 ************************************************************/
/**
* /data/data/存储图片
*
* @param context
* @param bitmap
* @param imageName
* 图片名
* @return 成功 false
*/
public static boolean saveDataImage(Context context, Bitmap bitmap,
String imageName) {
if (bitmap != null || isNullEmptyBlank(imageName)) {
try {
String path = context.getFilesDir().getAbsolutePath();
File file = new File(path, imageName);
if (file.exists()) {
file.delete();
}
OutputStream os = new FileOutputStream(file);
return saveOuToImage(bitmap, os);
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
/**
* /data/data/读取图片
*
* @param context
* @param imageName
* @return Bitmap
*/
public static Bitmap readDataImage(Context context, String imageName) {
if (isNullEmptyBlank(imageName)) {
try {
String path = context.getFilesDir().getAbsolutePath();
File file = new File(path, imageName);
if (file.exists()) {
InputStream in = new FileInputStream(file);
return BitmapFactory.decodeStream(in);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
return null;
}
/***************************** SDCard数据 *****************************************************************/
/**
* 写数据SDCard
*
* @param fileName
* 文件名
* @param writeStr
* 数据内容
* @return 成功 false
*/
public static boolean saveSDStr(String fileName, String writeStr) {
if (isNullEmptyBlank(fileName) || isNullEmptyBlank(writeStr)) {
try {
File file = new File(isExistsFilePath(), fileName);
if (file.exists()) {// 判读文件是否存在,存在读取
file.delete();
}
OutputStream os = new FileOutputStream(file);
return saveOuToStr(writeStr, os);
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
/**
* 读取SDCard文件
*
* @param fileName
* 文件名
* @return String 文件内容
*/
@SuppressLint("SdCardPath")
public static String readSDStr(String fileName) {
if (isNullEmptyBlank(fileName)) {
try {
String filePath = isExistsFilePath() + "/";// "/mnt/sdcard/image/"+
File file = new File(filePath);
if (file.exists()) {// 判读文件是否存在,存在读取
InputStream fin = new FileInputStream(filePath + fileName);
return readInToStr(fin);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/***************************** SDCard图片 *****************************************************************/
/**
* 保存到SDCard
*
* @param bitmap
* @param imageName
* 图片名
* @return 成功 false
*/
public static boolean saveSDImage(Bitmap bitmap, String imageName) {
if (isNullEmptyBlank(imageName) || bitmap != null) {
try {
File file = new File(isExistsFilePath(), imageName);
if (file.exists()) {// 判读文件是否存在,存在读取
file.delete();
}
OutputStream fos = new FileOutputStream(file);
return saveOuToImage(bitmap, fos);
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
/**
* 获取SDCard文件
*
* @param imageName
* 图片名
* @return Bitmap
*/
public static Bitmap readSDImage(String imageName) {
if (isNullEmptyBlank(imageName)) {
String filePath = getSDPath() + CACHE + "/" + imageName;
File file = new File(filePath);
if (file.exists()) {// 判读文件是否存在,存在读取
return BitmapFactory.decodeFile(filePath);
}
}
return null;
}
/***************************** 读取网络数据 *****************************************************************/
/**
* 读取网络数据
*
* @param path
* @return String
*/
public static String getNetFileString(String path) {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream inStream = conn.getInputStream();
return readData(inStream, CHARSETNAME);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static String readData(InputStream inSream, String charsetName)
throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int len = -1;
while ((len = inSream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inSream.close();
return new String(data, charsetName);
}
/***************************** 网络读取图片byte[] *****************************************************************/
/**
*
* @param path
* 文件路径
* @return byte[] data
*/
public static byte[] getNetImageByte(String path) {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream inStream = conn.getInputStream();
return readStream(inStream);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static byte[] readStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
outStream.close();
inStream.close();
return outStream.toByteArray();
}
/**
* 获取sd卡的缓存路径, 一般在卡中sdCard就是这个目录
*
* @return SDPath
*/
private static String getSDPath() {
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
if (sdCardExist) {
sdDir = Environment.getExternalStorageDirectory();// 获取根目录
} else {
Log.e("ERROR", "没有内存卡");
}
return sdDir.toString();
}
/**
* 获取缓存文件夹目录 如果不存在创建 否则则创建文件夹
*
* @return filePath
*/
private static String isExistsFilePath() {
String filePath = getSDPath() + CACHE;
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
return filePath;
}
private static boolean saveOuToStr(String writeStr, OutputStream os)
throws IOException, UnsupportedEncodingException {
os.write(writeStr.getBytes(CHARSETNAME));
os.flush();
os.close();
return false;
}
private static String readInToStr(InputStream in) throws IOException {
int length = in.available();
byte[] buffer = new byte[length];
in.read(buffer);
in.close();
return EncodingUtils.getString(buffer, CHARSETNAME);
}
private static boolean saveOuToImage(Bitmap bitmap, OutputStream fos)
throws IOException {
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
return false;
}
/**
* 判断字符串是否为空(包含null与""," ")
*
* @param str
* @return true 包含null与""," "
*/
private static boolean isNullEmptyBlank(String str) {
if (str == null || "".equals(str) || "".equals(str.trim())) {
return false;
}
return true;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。