截屏
public void screenshots() {
//截取的View
bindingView.Layout.post(new Runnable() {
@Override
public void run() {
View dView = bindingView.Layout;
dView.setDrawingCacheEnabled(true);
dView.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(dView.getDrawingCache());
if (bitmap != null) {
try {
//获取内置SD卡路径
String sdCardPath = Environment.getExternalStorageDirectory().getPath();
//图片文件路径
mFilePath = sdCardPath + File.separator + System.currentTimeMillis() + ".png";
File file = new File(mFilePath);
FileOutputStream os = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
os.close();
} catch (Exception e) {
}
}
}
});
}
想要保存到相册
try {
//传图片路径就ok
MediaStore.Images.Media.insertImage(getContentResolver(), mFilePath, "title", "description");
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(mFilePath))));
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}