uri转bitmap
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(mlist.get(i)));
uri转file
private File uri2File(Uri uri) {
String img_path;
String[] proj = {MediaStore.Images.Media.DATA};
Cursor actualimagecursor = this.managedQuery(uri, proj, null,
null, null);
if (actualimagecursor == null) {
img_path = uri.getPath();
} else {
int actual_image_column_index = actualimagecursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToFirst();
img_path = actualimagecursor
.getString(actual_image_column_index);
}
File file = new File(img_path);
return file;
}
bitmap转file
public static File compressImage(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 100;
while (baos.toByteArray().length / 1024 > 500) { //循环判断如果压缩后图片是否大于500kb,大于继续压缩 友盟缩略图要求不大于18kb
baos.reset();//重置baos即清空baos
options -= 10;//每次都减少10
bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
long length = baos.toByteArray().length;
}
SimpleDateFormat format = new SimpleDateFormat("HHmmss");
Date date = new Date(System.currentTimeMillis());
//图片名
String filename = format.format(date);
File file = new File(Environment.getExternalStorageDirectory(), filename + ".png");
try {
FileOutputStream fos = new FileOutputStream(file);
try {
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return file;
}