因為需要后臺默認截圖,所以特意搜了這個,然后做了一個這樣的DEMO.
第一個方法:(此方法只適用于Activity.不能截取應(yīng)用外的界面)代碼很少,這有這一段:
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) {
// 得到當(dāng)前view所在view結(jié)構(gòu)中的根view
View vv = v.getRootView();
// 設(shè)置屬性
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;
}
}
第二個方法(此方法可以截取應(yīng)用外的屏幕,即桌面或其他應(yīng)用):
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命令,可以放棄這個方法。
(正文已結(jié)束)
推薦閱讀:品麗潮流網(wǎng)
免責(zé)聲明及提醒:此文內(nèi)容為本網(wǎng)所轉(zhuǎn)載企業(yè)宣傳資訊,該相關(guān)信息僅為宣傳及傳遞更多信息之目的,不代表本網(wǎng)站觀點,文章真實性請瀏覽者慎重核實!任何投資加盟均有風(fēng)險,提醒廣大民眾投資需謹慎!