drawable to Stream - Android Graphics

Android examples for Graphics:Drawable Read

Description

drawable to Stream

Demo Code


//package com.java2s;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class Main {

    public static InputStream drawable2Stream(Drawable drawable) {
        Bitmap bitmap = drawable2Bitmap(drawable);
        return bitmap2Stream(bitmap);
    }//from   w  ww.ja  v  a  2  s  .co m

    public static Bitmap drawable2Bitmap(Drawable drawable) {
        return drawable == null ? null : ((BitmapDrawable) drawable)
                .getBitmap();
    }

    public static InputStream bitmap2Stream(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.JPEG, 100, baos);
        InputStream is = new ByteArrayInputStream(baos.toByteArray());
        return is;
    }
}

Related Tutorials