文件,base64字符串,Bitmap的相互转
————————————————
原文链接:https://blog.csdn.net/fengyeNom1/article/details/90024586
public class Base64Util {
public static void gcBitmap(Bitmap bitmap) {
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle(); // 回收图片所占的内存
bitmap = null;
System.gc(); // 提醒系统及时回收
}
}
/**
* Bitmap To Base64
*/
public static String bitmapToBase64(Bitmap bitmap) {
// 要返回的字符串
String reslut = null;
ByteArrayOutputStream baos = null;
try {
if (bitmap != null) {
baos = new ByteArrayOutputStream();
//todo 压缩只对保存有效果bitmap还是原来的大小
bitmap.compress(Bitmap.CompressFormat.JPEG, 30, baos);
baos.flush();
baos.close();
// 转换为字节数组
byte[] byteArray = baos.toByteArray();
// 转换为字符串
reslut = Base64.encodeToString(byteArray, Base64.NO_WRAP);
} else {
return null;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return reslut;
}
/**
* Bitmap To Bitmap
*/
public static Bitmap base64ToBitmap(String base64String) {
byte[] decode = Base64.decode(base64String, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
return bitmap;
}
/**
* Bitmap 转化为 二维数组
*/
public static byte[] getBytesByBitmap(Bitmap bitmap) {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(bitmap.getByteCount());
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
return outputStream.toByteArray();
} catch (Exception e) {
return new byte[10];
}
}
/**
* 文件 转 base64字符串
*/
public static String fileToBase64(File file) {
String base64 = null;
InputStream in = null;
try {
in = new FileInputStream(file);
byte[] bytes = new byte[in.available()];
int length = in.read(bytes);
base64 = Base64.encodeToString(bytes, 0, length, Base64.DEFAULT);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return base64;
}
/**
* base64 字符串 转 文件
*/
public static File base64ToFile(String base64) {
File file = null;
String fileName = "/Petssions/record/testFile.amr";
FileOutputStream out = null;
try {
// 解码,然后将字节转换为文件
file = new File(Environment.getExternalStorageDirectory(), fileName);
if (!file.exists()) {
file.createNewFile();
}
// 将字符串转换为byte数组
byte[] bytes = Base64.decode(base64, Base64.DEFAULT);
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
byte[] buffer = new byte[1024];
out = new FileOutputStream(file);
int bytesum = 0;
int byteread = 0;
while ((byteread = in.read(buffer)) != -1) {
bytesum += byteread;
// 文件写操作
out.write(buffer, 0, byteread);
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return file;
}
}