1

I get an error of

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
        at com.kotlin.ambulantlcs.helpers.PrinterHelper.getTrasparentBitmapCopy(PrinterHelper.java:145)
        at com.kotlin.ambulantlcs.helpers.PrinterHelper.convertStringToImage(PrinterHelper.java:133)

when I run this code in Android Java

public static void convertStringToImage(Activity activity)
    {
        Bitmap bitmap = Bitmap.createBitmap(300, 400, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.BLACK);
        Paint paint = new Paint();
        paint.setTextAlign(Paint.Align.LEFT);
        paint.setAntiAlias(true);
        paint.setTextSize(12);

        canvas.drawText("Lorem Ipsum Dolor Sed Actum Malo", 10, 10, paint);
        canvas.save();
        canvas.restore();

        int iRet = ftplib.printPicture(bitmap, FTPAndroidLib.DITHER_NONDITHERING, bitmap.getHeight(), bitmap.getWidth()); ===> ERROR IS HERE
       
    }

I am trying to create an image of a text that I create programmatically because I will be joining it to another image later. But before I do that, I am printing it out first.

Edit

Bitmap bitmap = Bitmap.createBitmap(200, 100, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);
        Paint paint = new Paint();
        paint.setTextAlign(Paint.Align.LEFT);
        paint.setAntiAlias(true);
        paint.setTextSize(9);
        canvas.drawPaint(paint);

        paint.setColor(Color.BLACK);
        paint.setTextSize(9);

        canvas.drawText("Lorem Ipsum Dolor Sed Actum Malo", 0, 50, paint);
        canvas.save();
        canvas.restore();

int iRet = ftplib.printPicture(bitmap, FTPAndroidLib.DITHER_NONDITHERING, bitmap.getWidth(), bitmap.getHeight());

Edit 2

 Bitmap bitmap = Bitmap.createBitmap(350, 200, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setTextAlign(Paint.Align.LEFT);
        paint.setAntiAlias(true);
        paint.setTextSize(20);

        Log.i("TAG", message);
        canvas.drawText("Lorem Ipsum Dolor Sed Actum Malo", 0, 50, paint);
        canvas.save();


         int iRet = ftplib.printPicture(bitmap, FTPAndroidLib.DITHER_NONDITHERING, bitmap.getWidth(), bitmap.getHeight());
1
  • I guess you restoring canvas earlier. Commented Oct 8, 2020 at 6:51

1 Answer 1

1

I hope it will help you, just try it.

    Bitmap bitmap = Bitmap.createBitmap(300, 400, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(Color.WHITE);  // White background color of canvas.
    Paint paint = new Paint();
    paint.setTextAlign(Paint.Align.LEFT);  // Text align left
    paint.setAntiAlias(true);
    paint.setTextSize(12);
    canvas.drawPaint(paint); 
    
    paint.setColor(Color.BLACK); // Black text color
    paint.setTextSize(20); 
    canvas.drawText("Put Your Text Here", 10, 10, paint);

    int iRet = ftplib.printPicture(bitmap, FTPAndroidLib.DITHER_NONDITHERING, bitmap.getHeight(), bitmap.getWidth());

    canvas.restore();
Sign up to request clarification or add additional context in comments.

3 Comments

I don't get the null anymore. Can you please make it that I get a white background and a black text? Thank you so munch.
change this value canvas.drawColor(Color.WHITE);
Please see my Edit. I get all black. Meaning, my canvas is black and so is the text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.