截屏+notification的小例子

android中截屏的关键代码:

//the view you want to shot
View view =activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bm = view.getDrawingCache();


notification主要的操作类是NotificationManager 和 notification:

一种方法,可以使用Builder创建:

NotificationManager manager = (NotificationManager) activity
.getSystemService(Context.NOTIFICATION_SERVICE);

Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://"+filePath),"image/*");
PendingIntent pi=PendingIntent.getActivity(activity, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Builder builder = new Builder(activity);
builder.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setContentText("save picture  success in " + filePath)
.setContentTitle("ScreenShot")
.setLargeIcon(
BitmapFactory.decodeResource(activity.getResources(),
android.R.drawable.stat_sys_download_done))
.setContentIntent(pi).setAutoCancel(true);
Notification n = builder.build();
manager.notify(0, n);


注意:如果要使用Builder创建Notification,应该导入包:

android.support.v4.app.NotificationCompat.Builder;否则会报错:could not find class android.app.notification$builder


还可以直接使用 Notification的构造方法:Notification n=new Notification();

Notification n = new Notification();
n.icon = android.R.drawable.stat_sys_download_done;
//n.largeIcon = BitmapFactory.decodeResource(activity.getResources(),
// android.R.drawable.stat_sys_download_done);

n.flags=Notification.FLAG_AUTO_CANCEL;
n.tickerText="screen shot success";
CharSequence contentTitle = "ScreenShot";
CharSequence contentText = "save picture  success in " + filePath;
//Intent intent = new Intent(activity, MainActivity.class);
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://"+filePath),"image/*");

PendingIntent contentIntent = PendingIntent.getActivity(activity, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
n.setLatestEventInfo(activity, contentTitle, contentText, contentIntent);

manager.notify(0, n);

具体代码如下:

package com.dream.supportandroid.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.net.Uri;
import android.support.v4.app.NotificationCompat.Builder;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

/**
 * a class to screen shot
 * @author annieLiu
 *
 */
public class ScreenShootUtil {
	private static Activity activity;
	
	/**
	 * 
	 * @param a
	 * @param file
	 */
	public static void shoot(Activity a, File file){
		if(file==null){
			showMsg("file not found");
			return ;
		}
		
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}
		activity = a;
		saveToPath(file);
	}

	/**
	 * 
	 * @param a
	 * @param filePath
	 */
	public static void shoot(Activity a, String filePath) {
		if (filePath == null || filePath.length() <= 0) {
			showMsg("file not found");
			return;
		}
		File file = new File(filePath);
		shoot(a, file);
	}

	/**
	 * save the picture to path
	 * @param file
	 */
	private static void saveToPath(File file) {
		//get the picture
		Bitmap bm = getScreenBitmap();

		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			bm.compress(CompressFormat.JPEG, 100, fos);
			Log.i("info",bm.toString());
			sendNotice(file.getAbsolutePath());

		} catch (IOException e) {
			showMsg("save failed,please try again");
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					showMsg("save failed,please try again");
				}
			}
		}

	}

	/**
	 * if save success,send a notification;and click notification will open the picture
	 * @param filePath
	 */
	@SuppressLint("NewApi")
	private static void sendNotice(String filePath) {
		NotificationManager manager = (NotificationManager) activity
				.getSystemService(Context.NOTIFICATION_SERVICE);

		Intent intent=new Intent(Intent.ACTION_VIEW);
		intent.setDataAndType(Uri.parse("file://"+filePath), "image/*");
		
		PendingIntent pi=PendingIntent.getActivity(activity, 0, intent, PendingIntent.FLAG_ONE_SHOT);
		
		Builder builder = new Builder(activity);
		builder.setSmallIcon(android.R.drawable.stat_sys_download_done)
				.setContentText("save picture  success in " + filePath)
				.setContentTitle("ScreenShot")
				.setLargeIcon(
						BitmapFactory.decodeResource(activity.getResources(),
								android.R.drawable.stat_sys_download_done))
								.setContentIntent(pi).setAutoCancel(true);
		Notification n = builder.build();
		
		manager.notify(0, n);
	}

	private static void showMsg(String msg) {
		Toast.makeText(activity,msg,
				Toast.LENGTH_LONG).show();
	}
	
	/**
	 * get the picture as bitmap
	 * @return
	 */
	private static Bitmap getScreenBitmap() {
		DisplayMetrics dm = activity.getResources().getDisplayMetrics();
		int screenW = dm.widthPixels;
		int screenY = dm.heightPixels;

		//the view you want to shot
		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap bm = view.getDrawingCache();
		
		//get the status bar's height
		Rect outRect = new Rect();
		activity.getWindow().getDecorView()
				.getWindowVisibleDisplayFrame(outRect);
		int statusHeight = outRect.top;

		//create a bitmap which is the view you want to shot without the status bar
		Bitmap b = Bitmap.createBitmap(bm, 0, statusHeight, screenW, screenY
				- statusHeight);
		//destroy the cache
		view.destroyDrawingCache();
		return b;
	}

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值