jiduvah
jiduvah

Reputation: 5168

Storing A Modified Bitmap To Disk

The project I am working on requires rounded corners (yes a copy from the iphone unfortunately). I round the corners when the image is downloaded then it is displayed. Here is how I round the corners...

public class ImageRounder {

    private Paint paint = new Paint();
    private Canvas canvas;
    private Rect rect;
    private RectF rectF;
    final int color = 0xff424242;

    @Inject
    public ImageRounder() {}

    public synchronized Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Bitmap.Config.ARGB_8888);
        canvas = new Canvas(output);
        paint.reset();
        rect =  new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        rectF = new RectF(rect);
        float roundPx = pixels;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.BLACK);

        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;

    }
}

I also save the images to disk so I don't have to download it more than onces. This is how I save and retrieve the image

@Override
public synchronized void saveImage(String id, Bitmap bitmap) throws FileNotFoundException       {
    FileOutputStream outputStream = context.openFileOutput(id, Context.MODE_PRIVATE);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
}

@Override
public synchronized Bitmap getImage(String id) throws FileNotFoundException {
    return BitmapFactory.decodeStream(context.openFileInput(id));
}

The problem is when I retrieve the image from disk it looks like this...

enter image description here

Its hard to tell me the corners are rounded but the background is black. I have tried setting the background of the imageview to both white and transparent but the corners still show as black.

Does anybody know why this is?

Upvotes: 0

Views: 312

Answers (1)

flav
flav

Reputation: 593

In saveImage, you compress the bitmap in JPEG.

For transparency, you must use Bitmap.CompressFormat.PNG

Upvotes: 1

Related Questions