保存scrollview布局视图到手机
1.获取布局bitmap
/**
* 获取布局bitmap
*
* @param view scrollview子布局
* @return
/
public static Bitmap loadBitmapFromView(View view) {
Bitmap bmp = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmp);
c.drawColor(Color.WHITE);
view.draw©;
return bmp;
}
2. 保存图片到手机并更新图库
/*
* 保存图片到手机相册,并通知图库更新
*
* @param context
* @param bmp 图片bitmap
*/
public static void saveImageToGallery(Context context, Bitmap bmp) {
// 首先保存图片
File appDir = new File(Environment.getExternalStorageDirectory(), “saveImage”);
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = System.currentTimeMillis() + “.jpg”;
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 最后通知图库更新
String path = Environment.getExternalStorageDirectory() + “/saveImage/” + fileName;
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(“file://” + path)));
}