Skip to content Skip to sidebar Skip to footer

Bitmap Is Not Saving Properly Only Black Image

I am trying to save custom view as image but this is not happening it only gives black image when i open it in android device gallery. The custom view is like this public class pag

Solution 1:

Update your saveautograph method as follows:

publicvoidsaveautograph()
{
    pg.setDrawingCacheEnabled(true);
    pg.buildDrawingCache();
    Bitmap returnedBitmap = pg.getDrawingCache();
    try {
           FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory()+"/autograph.jpeg");
           returnedBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
           out.close();
    } catch (Exception e) {
           e.printStackTrace();
    }
    pg.setDrawingCacheEnabled(false);
}

Basically, you can get the view as bitmap using the getDrawingCache method. Then directly save the bitmap to file..

Solution 2:

I believe you are trying to save the signatures captured on a view to an image. I have something ready which I re-use in my projects.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Environment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.leelasofttech.android.pcm.R;
import com.leelasofttech.android.pcm.util.Constants;

publicclassDrawViewextendsView {

protectedstaticfinalStringTAG="DrawView";

protected Bitmap mBitmap;
protected Canvas mCanvas;
protected Path mPath;
protected Paint mBitmapPaint;
protected Paint paint;
protectedint bgColor;

publicDrawView(Context context) {
    super(context);

    mPath = newPath();
    mBitmapPaint = newPaint(Paint.DITHER_FLAG);
    bgColor = context.getResources().getColor(R.color.edit_box_normal);
    setupPaint();
}

publicDrawView(Context context, AttributeSet attributeSet) {
    super(context, attributeSet);

    mPath = newPath();
    mBitmapPaint = newPaint(Paint.DITHER_FLAG);
    bgColor = context.getResources().getColor(R.color.edit_box_normal);

    setupPaint();
}

@OverrideprotectedvoidonSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    mCanvas = newCanvas(mBitmap);
}

@OverrideprotectedvoidonDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
    canvas.drawPath(mPath, getPaint());
}

privatefloat mX, mY;
privatestaticfinalfloatTOUCH_TOLERANCE=4;

privatevoidtouchStart(float x, float y) {
    mPath.reset();
    mPath.moveTo(x, y);
    mX = x;
    mY = y;
}

privatevoidtouchMove(float x, float y) {
    floatdx= Math.abs(x - mX);
    floatdy= Math.abs(y - mY);
    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
        mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
        mX = x;
        mY = y;
    }
}

privatevoidtouchUp() {
    mPath.lineTo(mX, mY);
    // commit the path to our offscreen
    mCanvas.drawPath(mPath, getPaint());
    // kill this so we don't double draw
    mPath.reset();
}

@OverridepublicbooleanonTouchEvent(MotionEvent event) {
    floatx= event.getX();
    floaty= event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touchStart(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            touchMove(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            touchUp();
            invalidate();
            break;
    }
    returntrue;
}

protectedvoidsetupPaint() {
    paint = newPaint();
    paint.setAntiAlias(true);
    paint.setDither(true);
    paint.setColor(bgColor);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setStrokeWidth(4);
}

public String toJPEGFile(String folderName, String fileName) {
    Filefolder=newFile(Environment.getExternalStorageDirectory() + File.separator + Constants.APP_FOLDER + File.separator + folderName + File.separator);
    if (!folder.exists()) {
        folder.mkdirs();
    }

    Filefile=null;
    try {
        this.setDrawingCacheEnabled(true);
        file = newFile(folder.getPath() + File.separator + fileName + Constants.FILE_TYPE_JPEG);
        FileOutputStreamfos=newFileOutputStream(file);
        Bitmapbitmap=this.getDrawingCache();
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
        return file.getAbsolutePath();
    }
    catch (FileNotFoundException ex) {
        Log.e(TAG, ex.getMessage(), ex);
    }
    catch (IOException ex) {
        Log.e(TAG, ex.getMessage(), ex);
    }
    catch (Exception ex) {
        Log.e(TAG, ex.getMessage(), ex);
    }
    returnnull;
}

public Paint getPaint() {
    return paint;
}

publicvoidsetPaint(Paint paint) {
    this.paint = paint;
}

publicvoidchangePaintStroke(float stroke) {
    paint.setStrokeWidth(stroke);
}

publicvoidchangePaintColor(int newColor) {
    paint.setColor(newColor);
}
}

// Signature view

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Path;
import android.util.AttributeSet;


publicclassSignatureViewextendsDrawView {

publicSignatureView(Context context) {
    super(context);
    paint.setColor(Color.BLACK);
}

publicSignatureView(Context context, AttributeSet attributeSet) {
    super(context, attributeSet);
    paint.setColor(Color.BLACK);
}

@OverrideprotectedvoidonSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    mCanvas = newCanvas(mBitmap);
}

@OverrideprotectedvoidonDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
    canvas.drawPath(mPath, getPaint());
}

publicvoidresetSignatures() {
    mPath = newPath();
    invalidate();
}
}

Hope this helps.

Post a Comment for "Bitmap Is Not Saving Properly Only Black Image"