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);

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);
Intent intent = new Intent(Intent.ACTION_SEND);
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)));

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s