//截屏 private void screenshot() { // 获取屏幕 Bitmap bmp = ScreenUtil.snapShotWithoutStatusBar(this); if (bmp != null) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { if (hasExternalStoragePermission(mContext)) { File appDir = new File(Common.SCREEN_SHOT_PATH); if (!appDir.exists()) { appDir.mkdirs(); } File file = new File(appDir, "MyShareCode.jpg"); try { FileOutputStream fos = new FileOutputStream(file); bmp.compress(CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } //通知相册更新 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile(file))); ToastUtil.showMessage(mContext, "保存成功"); } else { ToastUtil.showMessage(mContext, "请打开读写内存卡权限"); } } else { ToastUtil.showMessage(mContext, "SD卡不可用"); } } }public static Bitmap snapShotWithoutStatusBar(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); // Rect frame = new Rect(); // activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); // int statusBarHeight = frame.top; // // int width = getScreenWidth(activity); // int height = getScreenHeight(activity); // Bitmap bp = null; // bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height // - statusBarHeight); // view.destroyDrawingCache(); return bmp; }public static final String SCREEN_SHOT_PATH =Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath()+"/yourname";
保存图片到相册
最新推荐文章于 2025-12-18 15:08:42 发布
本文介绍了一个简单的Android应用程序中实现屏幕截图的方法。通过使用特定的Activity和屏幕捕获工具,可以轻松地创建不包含状态栏的屏幕快照,并将其保存到SD卡上指定的文件夹中。此外,还介绍了如何请求外部存储权限以及如何通知系统更新相册。
1694

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



