1.截图(相当于android手机同时按关机键+音量下键;不同之处就是截的图不带头部局上的信息栏),截下的图可打印
2.分享,把截的图分享到第三方平台微信、QQ等(这里用系统分享)
截图代码:就是把图片以流的形式保存到文件夹下,然后去标题即可(对图片处理)
public class ScreenShot { public static void shoot(Activity a, File filePath) { if (filePath == null) { return; } if (!filePath.getParentFile().exists()) { filePath.getParentFile().mkdirs(); } FileOutputStream fos = null; try { fos = new FileOutputStream(filePath); if (null != fos) { takeScreenShot(a).compress(Bitmap.CompressFormat.PNG, 100, fos);//保存的格式 } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } private static Bitmap takeScreenShot(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; int width = activity.getWindowManager().getDefaultDisplay().getWidth(); int height = activity.getWindowManager().getDefaultDisplay() .getHeight(); // 去掉标题栏 Bitmap b = Bitmap.createBitmap(bitmap, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return b; } }
2.调用1来保存图片(上面写好的工具类可以直接用,网上也特别多)
String filePath = Environment.getExternalStorageDirectory() + "/DCIM/"+ "Screen.png";//保存内存地址 ScreenShot.shoot(MainActivity.this,new File(filePath));//调用1
3.分享到第三方(调用系统的)
String shareStr = mShareNormalStr; File result = new File(filePath);//filePath用2保存的路径 Intent intent = new Intent("android.intent.action.SEND"); intent.setType("image/*"); intent.putExtra("sms_body", shareStr); intent.putExtra("android.intent.extra.TEXT", shareStr); intent.putExtra("android.intent.extra.STREAM", Uri.fromFile(result)); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(Intent.createChooser(intent, getResources().getString(R.string.share_to)));
分享前展示图:
分享后展示图: