Android截图

Android中截图很好实现,从开发文档中,可以看到View有一个接口getDrawingCache(),这个接口可以获取View在调用这个接口时的位图图像Bitmap。

截图是抓取View在某一个时刻的图像,包含了addView到这个View的所有子View的图像,比如在截取Activity时,图像是不会包含浮现在activity上方的对话框的

下面的代码是截取图像并且经过http post接口上传到服务器的例子,截图并上传的核心代码如下:

Screenshot.java

package com.example.scrmdemo;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;

public class Screenshot {

	public static Bitmap takeScreenshotForView(View view) {
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap tempBit = view.getDrawingCache();
		Rect frame = new Rect();
		view.getWindowVisibleDisplayFrame(frame);
		int width = view.getWidth();
		int height = view.getHeight();
		Bitmap bitmap = Bitmap.createBitmap(tempBit, 0, 0, width, height);
		view.destroyDrawingCache();
		return bitmap;
	}

	public static Bitmap takeScreenshotForActivity(Activity activity) {
		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap tempBit = view.getDrawingCache();
		Rect frame = new Rect();
		view.getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = frame.top;
		int width = view.getWidth();
		int height = view.getHeight();
		Bitmap bitmap = Bitmap.createBitmap(tempBit, 0, statusBarHeight, width,
				height - statusBarHeight);
		view.destroyDrawingCache();
		return bitmap;
	}

	public static Drawable BitmapToDrawable(Bitmap bitmap) {
		@SuppressWarnings("deprecation")
		BitmapDrawable bd = new BitmapDrawable(bitmap);
		Drawable drawable = (Drawable) bd;
		return drawable;
	}

	public static boolean savePic(Bitmap bitmap, String fileName) {
		try {
			File file = new File(fileName);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			FileOutputStream fos = null;
			fos = new FileOutputStream(file);
			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
			fos.flush();
			fos.close();
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	public static byte[] getBytes(Bitmap bitmap) {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
		try {
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return out.toByteArray();
	}

	interface ISharePicCallBack {
		public final static int SHARE_OK = 1;
		public final static int SHARE_NOTOK = 2;

		public void shareResult(int resultCode, String output);
	}

	public static void share(final String urlStr, final Bitmap bitmap,
			final ISharePicCallBack callBack) {
		new Thread() {
			public void run() {
				try {
					URL url = new URL(urlStr);
					HttpURLConnection httpConn = (HttpURLConnection) url
							.openConnection();
					httpConn.setDoOutput(true);
					httpConn.setDoInput(true);
					httpConn.setUseCaches(false);
					httpConn.setRequestMethod("POST");
					byte[] requestStringBytes = getBytes(bitmap);
					httpConn.setRequestProperty("Content-length", ""
							+ requestStringBytes.length);
					httpConn.setRequestProperty("Content-Type",
							"application/octet-stream");
					httpConn.setRequestProperty("Connection", "Keep-Alive");
					httpConn.setRequestProperty("Charset", "UTF-8");
					OutputStream outputStream = httpConn.getOutputStream();
					outputStream.write(requestStringBytes);
					outputStream.flush();
					outputStream.close();
					if (HttpURLConnection.HTTP_OK == httpConn.getResponseCode()) {
						StringBuffer sb = new StringBuffer();
						String readLine;
						BufferedReader responseReader;
						responseReader = new BufferedReader(
								new InputStreamReader(
										httpConn.getInputStream(), "UTF-8"));
						while ((readLine = responseReader.readLine()) != null) {
							sb.append(readLine).append("\n");
						}
						responseReader.close();
						callBack.shareResult(ISharePicCallBack.SHARE_OK,
								sb.toString());
					} else {
						callBack.shareResult(ISharePicCallBack.SHARE_NOTOK, ""
								+ httpConn.getResponseCode());
					}
				} catch (IOException e) {
					callBack.shareResult(ISharePicCallBack.SHARE_NOTOK, "");
					e.printStackTrace();
				}
			};
		}.start();
	}
}



使用本文件时,需要向Android工程的AndroidManifest.xml配置文件中添加两个权限:

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




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值