android v8 截屏(无需root无状态栏的2个方法)
android v8 截屏(无需root无状态栏的2个方法)
因为需要后台默认截图,所以特意搜了这个,然后做了一个这样的DEMO.
第一个方法:(此方法只适用于Activity.不能截取应用外的界面)代码很少,这有这一段:
import java.io.file; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.graphics.bitmap; import android.os.Bundle; import android.os.Environment; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; @SuppressLint("SetJavaScriptEnabled") public class MainActivity extends Activity { private Button leftStepBtn ; private Bitmap bm; private String imgurl; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imgurl = Environment.getExternalStorageDirectory() .getPath() "/DCIM/MyPicture/"; leftStepBtn = (Button) findViewById(R.id.leftStepBtn); leftStepBtn.setVisibility(View.VISIBLE); leftStepBtn.setText("截屏"); leftStepBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 得到当前view所在view结构中的根view View vv = v.getRootView(); // 设置属性 vv.setDrawingCacheEnabled(true); // 取得位图 bm = vv.getDrawingCache(); showConfirmDialog(1).show(); } }); } @SuppressLint("SdCardPath") public void saveBitmap(Bitmap bitmap String bitName) throws IOException { File folder = new File(imgurl); if (!folder.exists()) { folder.mkdir(); } File file = new File(imgurl bitName ".jpg"); Toast.makeText(MainActivity.this "保存图片中" Toast.LENGTH_SHORT).show(); FileOutputStream out; if (!file.exists()) { try { out = new FileOutputStream(file); if (bitmap.compress(Bitmap.CompressFormat.PNG 70 out)) { Toast.makeText(MainActivity.this "成功存入相册" Toast.LENGTH_SHORT).show(); out.flush(); out.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } /** * 确定保存 AlertDialog * @param i */ public AlertDialog showConfirmDialog(final int i) { return new AlertDialog.Builder(MainActivity.this) .setIcon(android.R.drawable.progress_horizontal) .setTitle("确认保存").setMessage("请确认是否保存图片!") .setPositiveButton("确定" new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog int which) { // TODO Auto-generated method stub try { if(i == 1){ String num = getRandomCode(); saveBitmap(bm "imagePic" num); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).setNegativeButton("取消" null).create(); } // 随机获取图片编号 private String getRandomCode() { String num = ""; for (int i = 0; i < 10; i ) { int f = (int) (Math.random() * 10); num = f; } return num; } // 按钮 @Override public boolean onKeyDown(int keyCode KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { finish(); return true; } return false; } }
第二个方法(此方法可以截取应用外的屏幕,即桌面或其他应用):
String imgurl = Environment.getExternalStorageDirectory() .getPath() "/DCIM/MyPicture/"; SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd_HH.mm.ss"); Date date = Calendar.getInstance().getTime(); String timeStamp = format.format(date); String fullPath = imgurl timeStamp ".png" ; ScreentShotUtil.getInstance().takeScreenshot(context fullPath);
ScreentShotUtil为一个shell命令的封装=-=如果你的机器不能用shell命令,可以放弃这个方法。