完成完整的源如..
private void snapScreen() {
View v1 = getWindow().getDecorView().getRootView();
// View v1 = iv.getRootView(); //even this works
View v1 = findViewById(R.id.main_result); //this works too for particular view
// but gives only content
Bitmap myBitmap;
v1.setDrawingCacheEnabled(true);
myBitmap = v1.getDrawingCache();
snap.saveBitmap(myBitmap, IntentResultItem);
}
對於創建快照
確保您同意外部存儲
public class SendSnap {
Context mContext;
public SendSnap(Context context) {
this.mContext = context;
}
public void saveBitmap(Bitmap bitmap, String msg) {
String filePath = Environment.getExternalStorageDirectory()
+ File.separator + "screenshot.png";
File imagePath = new File(filePath);
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
}