一、view转Bitmap 1、使用LayoutInflater导入布局
View view = LayoutInflater.from(activity).inflate(R.layout.img_qrcode, null, false);
2、测量计算view大小
//计算设备分辨率
WindowManager manager = activity.getWindowManager();
DisplayMetrics metrics = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
//测量使得view指定大小
int measureWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
int measureHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.AT_MOST);
view.measure(measureWidth, measureHeight);
//调用layout方法布局后,可以得到view的尺寸
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
3、把view转成图片Bitmap
final Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
view.draw(canvas);
二、将Bitmap保存到本地相册
/**
* 保存bitmap到相册
*/
private void saveBitmapToAlbum(Bitmap bitmap) {
final File appDir = new File(activity.getExternalCacheDir(), "image");
if (!appDir.exists()) {
appDir.mkdir();
}
final String fileName = System.currentTimeMillis() + ".jpg";
final File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
try {
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
} finally {
fos.close();
}
//把文件插入到系统相册
MediaStore.Images.Media.insertImage(activity.getContentResolver(), file.getAbsolutePath(), fileName, null);
//通知图库更新
//activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getAbsolutePath())));
activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(file.getPath()))));
} catch (Exception e) {
e.printStackTrace();
}
}
三、代码实现demo
public class SaveBitmap{
public void qrCode() {
View view = LayoutInflater.from(activity).inflate(R.layout.img_qrcode, null, false);
Bitmap saveBitmap = createBitmapByView(view);
saveBitmapToAlbum(saveBitmap);
}
/**
* view转bitmap
*
* @param view view
* @return Bitmap
*/
private Bitmap createBitmapByView(View view) {
//计算设备分辨率
WindowManager manager = activity.getWindowManager();
DisplayMetrics metrics = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
//测量使得view指定大小
int measureWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
int measureHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.AT_MOST);
view.measure(measureWidth, measureHeight);
//调用layout方法布局后,可以得到view的尺寸
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
final Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return bitmap;
}
/**
* 保存bitmap到相册
*/
private void saveBitmapToAlbum(Bitmap bitmap) {
final File appDir = new File(activity.getExternalCacheDir(), "image");
if (!appDir.exists()) {
appDir.mkdir();
}
final String fileName = System.currentTimeMillis() + ".jpg";
final File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
try {
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
} finally {
fos.close();
}
//把文件插入到系统相册
MediaStore.Images.Media.insertImage(activity.getContentResolver(), file.getAbsolutePath(), fileName, null);
//通知图库更新
//activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getAbsolutePath())));
activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(file.getPath()))));
} catch (Exception e) {
e.printStackTrace();
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)