0

I am trying to print a logo (an image) from my app to my bluetooth printer. I have searched online and especially through this site for a solution, but I still have not got a solution to my ultimate problem which is to print an image to a mini printer. There are many proffered answers, many of which I have had to adapt to solving my own problem, but I have still not been able to get it done. I used a sample code print_image(String File)- shown below which receives file path (which should reference a drawable image resource) as a string, but unfortunately, I get "File doesn't exist" error from my code because apparently, the application cannot properly reference the path (my biggest headache).

So, I need help on how to properly locate the path as a string and pass on to the print_image(String File) module. I have run into a brick wall on this one. Thank you.

private void print_image(String file) {
     File fl = new File(file);
     if (fl.exists()) {
         Bitmap bmp = BitmapFactory.decodeFile(file);
        convertBitmap(bmp);
        mService.write(PrinterCommands.SET_LINE_SPACING_24);

        int offset = 0;
        while (offset < bmp.getHeight()) {
            mService.write(PrinterCommands.SELECT_BIT_IMAGE_MODE);
            for (int x = 0; x < bmp.getWidth(); ++x) {

                for (int k = 0; k < 3; ++k) {

                    byte slice = 0;
                    for (int b = 0; b < 8; ++b) {
                        int y = (((offset / 8) + k) * 8) + b;
                        int i = (y * bmp.getWidth()) + x;
                        boolean v = false;
                        if (i < dots.length()) {
                            v = dots.get(i);
                        }
                        slice |= (byte) ((v ? 1 : 0) << (7 - b));
                    }
                    mService.write(slice);
                }
            }
            offset += 24;
            mService.write(PrinterCommands.FEED_LINE);
            mService.write(PrinterCommands.FEED_LINE);          
            mService.write(PrinterCommands.FEED_LINE);
            mService.write(PrinterCommands.FEED_LINE);
            mService.write(PrinterCommands.FEED_LINE);
            mService.write(PrinterCommands.FEED_LINE);
        }
        mService.write(PrinterCommands.SET_LINE_SPACING_30);


    } else {
        Toast.makeText(this, "file doesn't exists", Toast.LENGTH_SHORT)
                .show();
    }
}


public String getURLForResource (int resourceId) {
    return Uri.parse("android.resource://"+R.class.getPackage().getName()+"/" +resourceId).toString();
}

This is what I have done to call the print_image(File) 1. fileString = getURLForResource(R.drawable.imageName) 2. Pass the returned Uri (something like android.resource://com.myAppBase.com/4435664647) as a string 3. Call print_image(fileString)

So, at the point of testing if file exists in the print_image(file),

I get my message "file doesn't exists".
Please what am not doing right with getting the right path?

  • try this https://stackoverflow.com/questions/15540614/how-to-print-image-and-some-data-from-an-android-device-using-printer-print-vi – Sandeep dhiman Nov 21 '19 at 11:09

2 Answers2

0

Try to do this using Uri, then convert Uri to file InputStream,Then convert InputStream to Bitmap.

InputStream inputStream = getContentResolver().openInputStream(uri);

BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

Bitmap bmp = BitmapFactory.decodeStream(bufferedInputStream);

Then print the bitmap.

  • thanks for your response. Even though I am yet to try your recommendation but it seems to me that the URI I get (like this: android.resource://com.myAppBase.com/4435664647) is the problem. The app can't locate the image from the generate URI path. I assume that the image I am trying to reference is packaged along with my deployed app. I am doing something wrong while getting the Path? – George OAU Nov 21 '19 at 10:59
  • In android direct file access is no more happening. Instead you have to use InputStream/OutputStream . Further, there is a difference in File path and Uri. So ,as I had suggested, use the Uri to generate FileInputStream, use the FileStream to generate the Bitmap, and so on. – Rameswar Tarai Nov 21 '19 at 11:25
  • I have tried like you suggested, but the image is still not printing out. It just comes out blank. – George OAU Nov 21 '19 at 17:54
  • That is another issue.Your "file doesn't exists" issue is resolved.May I know what printer you are using? – Rameswar Tarai Nov 22 '19 at 05:15
  • I am using a Mini Bluetooth printer – George OAU Nov 22 '19 at 11:34
0
Bitmap bmp = BitmapFactory.decodeFile(file); 

You cannot use the File class for a resource. Better:

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.imageName);
blackapps
  • 8,011
  • 2
  • 11
  • 25