Retrieves byte data associated with the given Drawable - Android android.graphics.drawable

Android examples for android.graphics.drawable:Drawable

Description

Retrieves byte data associated with the given Drawable

Demo Code

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import java.io.ByteArrayOutputStream;

public class Main{

    /**//from  w w  w.  j a v a2  s  .  c om
     * Retrieves byte data associated with the given {@link Drawable}
     * 
     * @param drawable
     *            The {@link Drawable} instance
     * @return byte data which represents the {@link Drawable}
     */
    public static byte[] getImageByteData(final Drawable drawable) {
        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        return stream.toByteArray();
    }

}

Related Tutorials