您的位置:首页 > 其它

记录一些常用的utils方法

2015-09-30 16:02 316 查看
添加一些工具类,防止自己忘了,利于以后查看

一:把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;
}
}


以后会慢慢添加
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  utils工具类