スクリーンショットを撮ってギャラリーに保存するコードは次のとおりです。ここでデバッグしてみました画面はキャプチャされますが、ギャラリーには保存されません。
@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);
}
}