Check for network connection

This is how to check for an available data connection.

ConnectivityManager cm =
(ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm != null) {
// Connection found
} else {
// Handle no connection;
}

Bitmap to byte[]

Ok so how do I do this the other way round? Like so:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
myImageBitmap.compress(CompressFormat.PNG, 100, baos);
byte[] myImageBytes = baos.toByteArray();


byte[] to Bitmap

To create a Bitmap from a byte array you can use the super handy BitmapFactory

int offset = 0; // This is the point in the byte array that I want the decode to start from

int length = myByteArray.length; // This is the length of the byte array I want to decode

Bitmap myBitmap = BitmapFactory.decodeByteArray(myByteArray, offset, length);

Android Logging

How to print to Logcat for debugging purposes.

Log.d("TAG", "MESSAGE");

I like to define my tag as a constant and set up a filter in Logcat to read only that tag. This results in application specific Log streams.