Home > Android > Code snippets are useful in android

Code snippets are useful in android


1. Get bitmap from drawable directory

public Bitmap GetBitmap(int resourceID)
{
return BitmapFactory.decodeResource(context.getResources(), resourceID);
}

    2. Convert Image to Byte array

public byte[] Image2Byte(Bitmap bmp){
ByteArrayOutputStream out = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
return out.toByteArray();
}

    3. Convert Byte array to Image

public Bitmap Byte2Image(byte[] data){
Bitmap bmp;
bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
return bmp;
}

Just so easy. Happy coding!

  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: