将布局文件转化成Bitmap图片并保存到本地
-
获取windownwidth
int windowWidth = MyApplication.getWindowWidth();
-
将布局文件转化成Bitmap
public Bitmap getScrollViewBitmap(RelativeLayout relativeLayout) { int h = 0; Bitmap bitmap; h = relativeLayout.getHeight(); bitmap = Bitmap.createBitmap(windowWidth, h, Bitmap.Config.ARGB_4444); final Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.parseColor("#f2f7fa")); relativeLayout.draw(canvas); return bitmap; }
-
保存生成的Bitmap图片到本地
public String saveImageToGallery(Context context, Bitmap bmp) {
String path = Environment.getExternalStorageDirectory() + "/DAYU2/Screen";
File fileFolder = new File(path);
if (!fileFolder.exists()) {
fileFolder.mkdirs();
}
String fileName = "Screen_" + System.currentTimeMillis() + ".jpg";
File file = new File(fileFolder, 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();
}
return file.getAbsolutePath();
}
- 获取本地保存的Bitmap地址
public String shareBitmap() {
Bitmap xxBitmap = getScrollViewBitmap(comPictureOutRl);
if (xxBitmap == null) {
return null;
}
String s = saveImageToGallery(getContext(), xxBitmap);
return s;
}
网友评论