布局就是一个截图按钮,没有什么比较复杂的东西,直接上代码
方法一
1.在AndroidManifest中添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2.获取整改窗口的截图方法
/** * 获取整个窗口的截图 * * @param context * @return */ @SuppressLint("NewApi") private Bitmap captureScreen(Activity context) { View cv = context.getWindow().getDecorView(); cv.setDrawingCacheEnabled(true); cv.buildDrawingCache(); Bitmap bmp = cv.getDrawingCache(); if (bmp == null) { return null; } bmp.setHasAlpha(false); bmp.prepareToDraw(); return bmp; }
3.保存图片的方法
新建一个文件把图片保存到这个文件夹中,利用系统提供的api插入到系统相册,最后通知相册更新。具体方法如下:
public static void saveImageToGallery(Activity context, Bitmap bmp) { String local_path = Parser.getSdCard() + Const.PATH + Const.IMGAGE_PATH ; // 创建文件夹 // File appDir = new File(Environment.getExternalStorageDirectory(), "imageok"); File appDir = new File(local_path, "imageok"); //判断不存在就创建 if (!appDir.exists()) { appDir.mkdir(); } //以时间命名 String fileName = System.currentTimeMillis() + ".png"; File file = new File(appDir, fileName); try { FileOutputStream fos = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 其次把文件插入到系统图库 try { MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null); } catch (FileNotFoundException e) { e.printStackTrace(); } // 最后通知图库更新 String path = Environment.getExternalStorageDirectory().getPath(); context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path))); }
调用
//方法2 有的机型相册中无法找到图片暂时保存 // Bitmap bitmap = captureScreen(this); // saveImageToGallery(this,bitmap);方法二
/** * 截取全屏 * @return */ public Bitmap captureScreenWindow() { getWindow().getDecorView().setDrawingCacheEnabled(true); Bitmap bmp = getWindow().getDecorView().getDrawingCache(); return bmp; }
/** * 保存到内存卡 * * @param bitName * @param mBitmap */ public void saveBitmapForSdCard(Activity context, String bitName, Bitmap mBitmap) { String local_path = Parser.getSdCard() + Const.PATH + Const.IMGAGE_PATH; File appDir = new File(local_path); //判断不存在就创建 if (!appDir.exists()) { appDir.mkdir(); } //创建file对象 File f = new File(local_path + bitName + ".png"); try { //创建 f.createNewFile(); } catch (IOException e) { e.printStackTrace(); } FileOutputStream fOut = null; try { fOut = new FileOutputStream(f); } catch (FileNotFoundException e) { e.printStackTrace(); } //原封不动的保存在内存卡上 mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); try { fOut.flush(); } catch (IOException e) { e.printStackTrace(); } try { fOut.close(); } catch (IOException e) { e.printStackTrace(); } // 其次把文件插入到系统图库 try { MediaStore.Images.Media.insertImage(context.getContentResolver(), f.getAbsolutePath(), bitName, null); } catch (FileNotFoundException e) { e.printStackTrace(); } // 最后通知图库更新 String path = Environment.getExternalStorageDirectory().getPath(); context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path))); IToast.toast("截屏成功,请在相册中查看!"); }
调用方式
long time = System.currentTimeMillis() / 1000; Bitmap bitmap = captureScreenWindow(); saveBitmapForSdCard(this, time + "", bitmap);