当前这个功能发现不支持截取当前弹出的Dialog内容,后面换方法优化
public class ScreenImageUtils {
private UploadFinish uploadFinish;
public ScreenImageUtils(UploadFinish uploadFinish) {
this.uploadFinish = uploadFinish;
screenCaptureNoStatusBar();
}
/**
* 截取当前可见范围屏幕(不包含状态栏)
*/
private void screenCaptureNoStatusBar() {
View view = BaseActivity.getContext().getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
// 获取状态栏高度
Rect rect = new Rect();
view.getWindowVisibleDisplayFrame(rect);
int statusBarH = rect.top;
// 获取屏幕宽高
int w = view.getWidth();
int h = view.getHeight();
// 去掉状态栏
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache(), 0, statusBarH, w, h - statusBarH);
// 销毁缓存信息
view.destroyDrawingCache();
saveBitmapToFile(bitmap);
}
// 保存Bitmap到指定路径,如果文件已存在则替代
FileOutputStream out;
private void saveBitmapToFile(Bitmap bitmap) {
// 确保目录存在
File image = createImageFile();
// 尝试将Bitmap保存到文件
try{
out = new FileOutputStream(image);
// 第一个参数是压缩格式(PNG, JPEG等),第二个参数是质量(0-100),第三个是输出流
// 这里以JPEG格式保存,质量为100%
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
} catch (Exception e) {
e.printStackTrace();
try {
if(out!=null){
out.close();
}
}catch (Exception e1){
}
}finally {
try {
if(out!=null){
out.close();
}
String absolutePath = image.getAbsolutePath();
Log.e("JobScheduler",absolutePath);
//上传图片上传回调用jobFinished
uploadFinish.uploadImageFinish();
}catch (Exception e){
}
}
}
private File createImageFile(){
try {
String imageFileName = "AI_SD_SCREEN_IMAGE";
File storageDir = BaseActivity.getContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = new File(storageDir+"/"+imageFileName+".jpg");
return image;
}catch (Exception e){
Log.e("111",e.getMessage());
}
return null;
}
interface UploadFinish{
void uploadImageFinish();
}
}
Android 截屏当前View并保存本地(这个方法不支持Dialog内容)
最新推荐文章于 2025-11-24 00:24:56 发布
2434

被折叠的 条评论
为什么被折叠?



