android截屏

private void printScreen(boolean save) {//截屏
		View view = this.getWindow().getDecorView();//this是当前的Activity
		// if (view.isDrawingCacheEnabled()) {
		view.setDrawingCacheEnabled(true);
		Calendar c = Calendar.getInstance();
		String date = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH) + "  " + c.get(Calendar.HOUR_OF_DAY) + "-" + c.get(Calendar.MINUTE) + "-" + c.get(Calendar.SECOND);
		// }
		view.buildDrawingCache();
		Bitmap bmp = view.getDrawingCache();
		imagePath = ConstValue.MY_ALBUM_DIR + "/" + date + ".jpg";//路径
		writePhotoJpg(bmp, imagePath);
		// FileSaveAsync myAsync = new FileSaveAsync(bmp, imagePath, true);
		// myAsync.execute();
	}

保存到sd卡下的方法:(记得加上写入sd卡的权限)

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>

public void writePhotoJpg(Bitmap data, String pathName) {
		File file = new File(pathName);
		try {
			file.createNewFile();
			// BufferedOutputStream os = new BufferedOutputStream(
			// new FileOutputStream(file));

			FileOutputStream os = new FileOutputStream(file);
			data.compress(Bitmap.CompressFormat.JPEG, 100, os);
			os.flush();
			os.close();
			MyDebug.i("writePhotoJpg");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

public void writePhotoPng(Bitmap data, String pathName) {
		File file = new File(pathName);
		try {
			file.createNewFile();
			FileOutputStream os = new FileOutputStream(file);
			// BufferedOutputStream os = new BufferedOutputStream(
			// new FileOutputStream(file));
			data.compress(Bitmap.CompressFormat.PNG, 100, os);
			os.flush();
			os.close();
			MyDebug.i("writePhotoPng");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

另外几种方法,没仔细测试过:仅供参考

 //抓屏的方式生成照片
	   public static Bitmap printScreen(Context context) {
			View view = ((Activity) context).getWindow().getDecorView();
			// if (view.isDrawingCacheEnabled()) {
			view.setDrawingCacheEnabled(true);
		//	Calendar c = Calendar.getInstance();
		//	String date = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH) + "  " + c.get(Calendar.HOUR_OF_DAY) + "-" + c.get(Calendar.MINUTE) + "-" + c.get(Calendar.SECOND);
			// }
			view.buildDrawingCache();
			Bitmap bmp = view.getDrawingCache();
			return bmp;
			/*imagePath = ConstValue.MY_ALBUM_DIR + "/" + date + ".jpg";
			ImageFile.writePhotoJpg(bmp, imagePath);*/
			// FileSaveAsync myAsync = new FileSaveAsync(bmp, imagePath, true);
			// myAsync.execute();
		}
         public static Bitmap catchScreen(View v1) {
	        v1.setDrawingCacheEnabled(true);
	        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
	        v1.setDrawingCacheEnabled(false);
	        return bitmap;
          }



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值