获取当前屏幕,然后截屏,并保存在一定的目录下
1、需要获取SD权限
2、按power key来实现截屏
3、缺点是只能截取该APK的界面
package com.my.takeimage;
import java.io.File;
import java.io.FileOutputStream;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
public class MainActivity extends Activity {
private final BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_SCREEN_OFF.equals(action)) {
GetandSaveCurrentImage("test_001","jpg");
Log.e("TAG", "Screen turn off");
} else if (Intent.ACTION_SCREEN_ON.equals(action)) {
Log.e("TAG", "Screen turn on");
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
IntentFilter filter1 = new IntentFilter(Intent.ACTION_SCREEN_ON);
registerReceiver(mBatInfoReceiver, filter);
registerReceiver(mBatInfoReceiver, filter1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onDestory() {
if (mBatInfoReceiver != null) {
try {
unregisterReceiver(mBatInfoReceiver);
} catch (Exception e) {
Log.e("TAG", "unregisterReceiver mBatInfoReceiver failure :"
+ e.getCause());
}
}
}
private void GetandSaveCurrentImage(String name,String format)
{
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
int w = display.getWidth();
int h = display.getHeight();
Bitmap Bmp = Bitmap.createBitmap( w, h, Config.ARGB_8888 );
View decorview = this.getWindow().getDecorView();
decorview.setDrawingCacheEnabled(true);
Bmp = decorview.getDrawingCache();
String SavePath = getSDCardPath()+"/AndyDemo/ScreenImage";
try {
File path = new File(SavePath);
String filepath = SavePath + "/" + name + "." + format;
File file = new File(filepath);
if(!path.exists()){
path.mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = null;
fos = new FileOutputStream(file);
if (null != fos) {
Bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
Toast.makeText(this, "截屏文件已保存至SDCard/AndyDemo/ScreenImage/下", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private String getSDCardPath(){
File sdcardDir = null;
boolean sdcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(sdcardExist){
sdcardDir = Environment.getExternalStorageDirectory();
}
return sdcardDir.toString();
}
}