话不多说,直接上代码!
//保存图片到相册
private void createPicture() {
// 2. 将布局转成bitmap
View decorView = this.getWindow().getDecorView().findViewById(R.id.ll_result_poster);
Bitmap bitmap =viewToBitmap(decorView);
//3. 将bitmap存入本地,test文件夹,可自定义
String strPath = "/test/" + UUID.randomUUID().toString() + ".jpg";
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File sdCardDir = Environment.getExternalStorageDirectory();
FileOutputStream fos = null;
try {
File file = new File(sdCardDir, strPath);
Uri uri = Uri.fromFile(file);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
fos = new FileOutputStream(file);
//当指定压缩格式为PNG时保存下来的图片显示正常
bi