Kæmpe Klunker
Kæmpe Klunker

Reputation: 865

Drawable to Bitmap

I have an app whit an imageView, which is set by the user, either using his/her camera or file system. When the image is set I use this class to round the corners of the image:

public class DrawRoundImage extends Drawable {
    private static final boolean USE_VIGNETTE = true;

    private final float mCornerRadius;
    private final RectF mRect = new RectF();
    private final BitmapShader mBitmapShader;
    private final Paint mPaint;
    private final int mMargin;

    public DrawRoundImage(Bitmap bitmap, float cornerRadius, int margin) {
        mCornerRadius = cornerRadius;

        mBitmapShader = new BitmapShader(bitmap,
                Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setShader(mBitmapShader);

        mMargin = margin;
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        mRect.set(mMargin, mMargin, bounds.width() - mMargin, bounds.height() - mMargin);

        if (USE_VIGNETTE) {
            RadialGradient vignette = new RadialGradient(
                    mRect.centerX(), mRect.centerY() * 1.0f / 0.7f, mRect.centerX() * 1.3f,
                    new int[] { 0, 0, 0x7f000000 }, new float[] { 0.0f, 0.7f, 1.0f },
                    Shader.TileMode.CLAMP);

            Matrix oval = new Matrix();
            oval.setScale(1.0f, 0.7f);
            vignette.setLocalMatrix(oval);

            mPaint.setShader(
                    new ComposeShader(mBitmapShader, vignette, PorterDuff.Mode.SRC_OVER));
        }
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawRoundRect(mRect, mCornerRadius, mCornerRadius, mPaint);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    @Override
    public void setAlpha(int alpha) {
        mPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        mPaint.setColorFilter(cf);
    }
}

Now I need to upload the image to my server, but since it is a drawable I can't quite figure how to turn it back to a Bitmap, which I convert like this:

Bitmap bitmap = bMap;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

I try to convert the drawable to a Bitmap like this:

Drawable drawable = imageButton1.getDrawable();
Bitmap bitmap  = ((BitmapDrawable) drawable).getBitmap();

But it throws this error:

java.lang.ClassCastException: com.adrissa.klea.model.DrawRoundImage cannot be cast to android.graphics.drawable.BitmapDrawable

How can i properly convert a drawable to a bitmap? When i google all i find is questions about drawable resources to bitmap.

Upvotes: 0

Views: 400

Answers (2)

wpiwonski
wpiwonski

Reputation: 1284

@Kæmpe Klunker, look at this solution:

https://stackoverflow.com/a/10600736/4137318

Probably you have to override following methods in your DrawRoundImage class, to make this solution working:

@Override
public int getIntrinsicHeight() {
    return (int) mRect.height();
}

@Override
public int getIntrinsicWidth() {
    return (int) mRect.width();
}

Upvotes: 0

Amirhossein Naghshzan
Amirhossein Naghshzan

Reputation: 1180

Here is how you can convert drawable to bitmap:

Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.example);

Upvotes: 1

Related Questions