添加一些工具类,防止自己忘了,利于以后查看
一:把bitmap进行保存
/**
* @param getimage 图片
* @param path 路径
* @param format 图片以什么格式保存
* @param quality 图片以什么质量保存
* @return File 图片保存后的文件
*/
public static File saveBitmapToFile(Bitmap getimage, String path, Bitmap.CompressFormat format, int quality) {
final File file = new File(path);
FileOutputStream fos = null;
try {
if (file.exists() && file.delete()) {
}
if (file.createNewFile()) {
fos = new FileOutputStream(file);
getimage.compress(format, quality, fos);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
}
}
}
return file;
}
二:bitmap生成base64
/**
* @param imgPath 图片地址
* @return
*/
public static String imgToBase64(String imgPath) {
Bitmap bitmap = null;
if (imgPath !=null && imgPath.length() > 0) {
bitmap = readBitmap(imgPath);
}
if(bitmap == null){
Log.w("图片不存在","");
}
ByteArrayOutputStream out = null;
try {
out = new ByteArrayOutputStream();
if(bitmap != null){
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
}
out.flush();
out.close();
byte[] imgBytes = out.toByteArray();
return Base64.encodeToString(imgBytes, Base64.DEFAULT);
} catch (Exception e) {
// TODO Auto-generated catch block
return null;
} finally {
try {
if(out != null) {
out.flush();
out.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static Bitmap readBitmap(String imgPath) {
try {
return BitmapFactory.decodeFile(imgPath);
} catch (Exception e) {
// TODO Auto-generated catch block
return null;
}
}
三:base64生成bitmap
/**
*
* @param base64Data base64
* @return
*/
public static Bitmap stringToBitmap(String base64Data){
//将字符串转换成Bitmap类型
Bitmap bitmap=null;
try {
byte[] bitmapArray;
bitmapArray=Base64.decode(base64Data, Base64.DEFAULT);
bitmap=BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
四:判断是单击,还是双击
public class DoubleKUtils {
private static long lastClickTime = 0;
private static long Time_Interval = 500;
public static boolean isFastDoubleClick() {
long curTime = System.currentTimeMillis();
long interval = curTime - lastClickTime;
if (0 < interval && interval < Time_Interval) {
return true;
}
lastClickTime = curTime;
return false;
}
public static void setTimeInterval(long time) {
Time_Interval = time;
}
}
以后会慢慢添加
实用Bitmap操作技巧
本文介绍了几种关于Bitmap的重要操作,包括将Bitmap保存为文件的方法、Bitmap与Base64字符串之间的相互转换,以及如何通过时间间隔判断用户的点击行为是单次还是双击。
4185

被折叠的 条评论
为什么被折叠?



