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;
}
}