我有以下方法可以在操作项单击时捕获屏幕。它适用于 Android <2.3 但不适用于 4+。这种截屏方式有什么问题。
private void captureScreen() {
View v = mapView.getRootView();
v.setDrawingCacheEnabled(true);
Bitmap capturedBitmap = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
if(capturedBitmap != null) {
Intent intent = new Intent(this, ScreenCapturedAlertActivity.class);
intent.putExtra("capturedImage", capturedBitmap);
intent.putExtra("name", location.getName());
startActivity(intent);
} else {
Toast.makeText(this, "Screen Capture Failed", Toast.LENGTH_SHORT).show();
}
}
ScreenCaputureAlertActivity.java >>>
public class ScreenCapturedAlertActivity extends SherlockActivity {
private ImageView capturedImage;
private Bitmap capturedBitmap;
private String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_screencaptured_alert);
capturedBitmap = (Bitmap) getIntent().getParcelableExtra("capturedImage");
name = getIntent().getStringExtra("name");
capturedImage = (ImageView) findViewById(R.id.ivCapturedImage);
capturedImage.setImageBitmap(capturedBitmap);
}
private void saveAndShare(boolean share) {
String root = Environment.getExternalStorageDirectory().toString();
File dir = new File(root + "/capture/");
if(!dir.exists())
dir.mkdirs();
FileOutputStream outStream = null;
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
File file = new File(dir, "Capture "+n+".jpg");
if(file.exists()) {
file.delete();
}
try {
outStream = new FileOutputStream(file);
capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
return;
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
return;
}
if(share) {
Uri screenshotUri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
intent.putExtra(Intent.EXTRA_SUBJECT, "Location of " + name);
intent.putExtra(Intent.EXTRA_TITLE, getText(R.string.screen_share_message));
intent.putExtra(Intent.EXTRA_TEXT, getText(R.string.screen_share_message));
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share with"));
finish();
} else {
Toast.makeText(this, "Save Success", Toast.LENGTH_SHORT).show();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://" + Environment.getExternalStorageDirectory())));
finish();
}
}
public void saveCapture(View view) {
saveAndShare(false);
}
public void shareCapture(View view) {
saveAndShare(true);
}
}