user1166635
user1166635

Reputation: 2801

Can't save Bitmap as JPEG

I have a Bitmap object which is used for drawing, and I want to save image from it on the SDCARD using JPEG format. I have the following code:

    public void saveBitmap() throws IOException {

        String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/output.jpg";
        File output=new File(path);

        BufferedOutputStream ous = null;
        try {
            ous=new BufferedOutputStream(new FileOutputStream(output));
            mBitmap.compress(CompressFormat.JPEG, 100, ous);
            ous.flush();
            ous.close();
        } finally {
            if (ous!=null) {
                try {
                    ous.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Log.e("closing", e.getMessage());
                }
            }
        }
    }

But after execting this function I can see jpeg file with a black background always. If I change the format to PNG the all will be OK. Where have I made a mistake?

Code for drawing:

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawColor(0x00FFFFFF);
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        canvas.drawPath(mPath, mPaint);
    }

Upvotes: 1

Views: 493

Answers (1)

HandlerExploit
HandlerExploit

Reputation: 8251

The jpeg format doesn't support transparency. That is why it renders transparency to black when saved.

Upvotes: 1

Related Questions