Skip to content

Instantly share code, notes, and snippets.

@hayukleung
Created March 8, 2017 10:28
Show Gist options
  • Save hayukleung/a2396d19e37d038a294660616bb7664a to your computer and use it in GitHub Desktop.
Save hayukleung/a2396d19e37d038a294660616bb7664a to your computer and use it in GitHub Desktop.
How to Capture Screen and Share It.
public class How2CaptureScreenAndShareIt {
private void draw(float scaleW, float scaleH) {
File dest = AndroidUtils.getTmpFile();
FileOutputStream fos = null;
try {
final Bitmap bmp =
createBitmap((int) (mRoot.getWidth() * scaleW), (int) (mRoot.getHeight() * scaleH),
Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bmp);
canvas.scale(scaleW, scaleH);
mRoot.draw(canvas);
fos = new FileOutputStream(dest);
bmp.compress(Bitmap.CompressFormat.JPEG, 80, fos);
bmp.recycle();
fos.flush();
MediaStore.Images.Media.insertImage(getActivity().getContentResolver(),
dest.getAbsolutePath(), App.getAccountUser().getNickname() + "行程规划", null);
getActivity().sendBroadcast(
new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(dest)));
Toost.message(getActivity().getString(R.string.picture_saved_to_storage, "相册"));
// 直接分享行程规划截图
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpg");
Uri uri = Uri.fromFile(dest);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.putExtra(Intent.EXTRA_SUBJECT, "行程规划");
intent.putExtra(Intent.EXTRA_TEXT, "行程规划");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, "分享至"));
} catch (OutOfMemoryError e) {
Ln.e(e);
draw(scaleW * 0.9f, scaleH * 0.9f);
} catch (FileNotFoundException e) {
Ln.e(e);
Toost.message("保存失败");
} catch (IOException e) {
Ln.e(e);
Toost.message("保存失败");
} finally {
IoUtils.safeClose(fos);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment