Android把bitmap转为file类型 ,图片转base64**
Android后台需要file文件,把bitmap转为file类型
直接上代码
//将Bitmap类型的图片转化成file类型,便于上传到服务器
public File saveFile(Bitmap bm) throws IOException {
String fileName = new Date().getTime() + “”;
String path = Environment.getExternalStorageDirectory() + “/cads”;
File dirFile = new File(path);
if (!dirFile.exists()) {
dirFile.mkdir();
}
File myCaptureFile = new File(path + fileName);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();
bos.close();
return myCaptureFile;
}
/**
- 吧图片转为base64
*/
public static String imageToBase64(Bitmap bitmap) {
String result = null;
ByteArrayOutputStream baos = null;
try {
if (bitmap != null) {
baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
baos.flush();
baos.close();
byte[] bitmapBytes = baos.toByteArray();
result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
baos.flush();
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
下一篇: 动态添加文本框