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?