把手机上的一个View或ViewGroup转为Bitmap,再把Bitmap保存为.png格式的图片;
由于View转Bitmap、和Bitmap转图片都是耗时操作,(生成一个1M的图片大约500ms,如果图片过大,用户会觉得APP卡顿,甚至ANR)我在子线程进行处理,然后把保存的结果回调出来;
监听回调分别是: 开始、成功、失败、完成; 可以在各个回调中做处理;
由于用到了读写本地文件的权限,记得给该APP分配权限;
<!-- SDCard创建删除文件 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- SDCard读写 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
6.0动态权限:
private boolean storagePermission(){
//动态获取内存存储权限
int permission = ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
1);
return false;
}else{
return true;
}
}
如何使用:
ViewSaveImageUtils.savePhotoToSDCard(scrollView, new ViewSaveImageUtils.OnSaveListEner() {
@Override
public void onStart() {
runOnUiThread(() -> showProgressDialog("生成图片...")); //显示dialog
L.cc("开始");
}
@Override
public void onSucceed(String filePath) {
L.cc("成功:"+filePath);
}
@Override
public void onFailure(String error) {
L.cc("失败"+error);
}
@Override
public void onFinish() {
L.cc("结束");
runOnUiThread(() -> dismissProgressDialog()); //隐藏dialog
}
});
View转为图片文件的工具类,注释很详细,不多解释了;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Environment;
import android.view.View;
import android.widget.ScrollView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* ChenboCui View转Bitmap 把图片保存到本地的工具类
*/
public class ViewSaveImageUtils {
/**
* 把一个View转为图片保存到本地
* @param view
* @param ener
*/
public static void viewSaveToImage(View view, OnSaveListEner ener) {
if (ener != null) ener.onStart();
// 把一个View转换成图片
Bitmap cachebmp = loadBitmapFromView(view);
FileOutputStream fos;
if (checkSDCardAvailable()) {
try {
// SD卡根目录
File sdRoot = AndroidUtils.getDownloadDir();
File file = new File(sdRoot, System.currentTimeMillis() + ".png");
fos = new FileOutputStream(file);
cachebmp.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
if (ener != null) ener.onSucceed(file.getAbsolutePath());
} catch (Exception e) {
if (ener != null) ener.onFailure(e.getMessage());
e.printStackTrace();
}
} else {
if (ener != null) ener.onFailure("创建文件失败!");
}
view.destroyDrawingCache();
if (ener != null) ener.onFinish();
}
/**
* 把一个View转为图片保存到本地
* @param view
* @return
*/
public static String viewSaveToImage(View view) {
// 把一个View转换成图片
Bitmap cachebmp = loadBitmapFromView(view);
FileOutputStream fos;
String imagePath = "";
try {
if (checkSDCardAvailable()) {
// SD卡根目录
File sdRoot = AndroidUtils.getDownloadDir();
File file = new File(sdRoot, System.currentTimeMillis() + ".png");
fos = new FileOutputStream(file);
imagePath = file.getAbsolutePath();
} else {
throw new Exception("创建文件失败!");
}
cachebmp.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
L.cc("imagePath=" + imagePath);
view.destroyDrawingCache();
return imagePath;
}
/**
* 把一个View转为Bitmap
*
* @param v
* @return
*/
public static Bitmap loadBitmapFromView(View v) {
v.setDrawingCacheEnabled(true);
v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
v.setDrawingCacheBackgroundColor(Color.WHITE);
int w = v.getWidth();
int h = v.getHeight();
Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmp);
c.drawColor(Color.WHITE);
/** 如果不设置canvas画布为白色,则生成透明 */
v.layout(0, 0, w, h);
v.draw(c);
return bmp;
}
/**
* Bitmap保存为图片
*
* @param photoBitmap
* @param path
* @param photoName
*/
public static void savePhotoToSDCard(Bitmap photoBitmap, String path, String photoName) {
if (checkSDCardAvailable()) {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
File photoFile = new File(path, photoName + ".png");
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(photoFile);
if (photoBitmap != null) {
if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)) {
fileOutputStream.flush();
}
} else {
}
} catch (FileNotFoundException e) {
photoFile.delete();
e.printStackTrace();
} catch (IOException e) {
photoFile.delete();
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* ScrollView保存为图片
*
* @param scrollView
* @param path
* @param photoName
*/
public static void savePhotoToSDCard(ScrollView scrollView, String path, String photoName, OnSaveListEner ener) {
//耗时操作,放线程处理
new Thread(new Runnable() {
@Override
public void run() {
if (ener != null) ener.onStart();
if (checkSDCardAvailable()) {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
File photoFile = new File(path, photoName + ".png");
FileOutputStream fileOutputStream = null;
try {
Bitmap photoBitmap = getBitmapByView(scrollView);
fileOutputStream = new FileOutputStream(photoFile);
if (photoBitmap != null) {
if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)) {
fileOutputStream.flush();
}
if (ener != null) ener.onSucceed(photoFile.getAbsolutePath());
} else {
if (ener != null) ener.onFailure("Bitmap = NULL");
}
} catch (FileNotFoundException e) {
if (ener != null) ener.onFailure("FileNotFoundException");
photoFile.delete();
e.printStackTrace();
} catch (IOException e) {
if (ener != null) ener.onFailure("IOException");
photoFile.delete();
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
if (ener != null) ener.onFailure("文件储存异常");
}
if (ener != null) ener.onFinish();
}
}).start();
}
public static void savePhotoToSDCard(ScrollView scrollView, OnSaveListEner ener) {
String path = AndroidUtils.getDownloadDir().getAbsolutePath();
String photoname = "maxus" + System.currentTimeMillis();
savePhotoToSDCard(scrollView, path, photoname, ener);
}
/**
* 把一个View转为Bitmap
*
* @param scrollView
* @return
*/
public static Bitmap getBitmapByView(ScrollView scrollView) {
int h = 0;
Bitmap bitmap = null;
for (int i = 0; i < scrollView.getChildCount(); i++) {
h += scrollView.getChildAt(i).getHeight();
}
bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
Bitmap.Config.RGB_565);
final Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE); //白色背景
scrollView.draw(canvas);
return bitmap;
}
public static boolean checkSDCardAvailable() {
return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}
public interface OnSaveListEner {
void onStart();
void onSucceed(String filePath);
void onFailure(String error);
void onFinish();
}
}
AndroidUtils.getDownloadDir(); 也是我的一个工具类,我把这个方法贴下来:
public static File getDownloadDir() {
File downloadDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
if (!downloadDir.exists()) {
downloadDir.mkdir();
}
return downloadDir;
}
好了,大功告成!!!