这是我拍摄屏幕截图并将其保存在画廊中的代码。通过调试在这里尝试捕获屏幕但不保存到画廊。
@Override
public void onClick(View v) {
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap);
}
制作截图
protected Bitmap takeScreenshot() {
// TODO Auto-generated method stub
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
Toast.makeText(getApplicationContext(), "Screen captured", Toast.LENGTH_LONG).show();
return rootView.getDrawingCache();
}
在此处保存屏幕
protected void saveBitmap(Bitmap bitmap) {
// TODO Auto-generated method stub
File imagePath = new File(Environment.getExternalStorageDirectory()
+ "/screenshot.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
Toast.makeText(getApplicationContext(), "Screen saved", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}