Getting a bitmap from a view

Getting a bitmap from a view is very easy in Android (as long as it has already been laid out on the screen):

Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
view.draw(canvas);

Then you can easily save it to the gallery or email/fb/whatever:

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/mydirectory/myfilename.png");
FileOutputStream fio = new FileOutputStream(file, false);
bitmap.compress(CompressFormat.PNG, 100, fio);
fio.close();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_SUBJECT, "my subject");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
context.startActivity(Intent.createChooser(intent, context.getString(R.string.intentchooser_share_title)));

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s