react-native-esc-pos-printer
react-native-esc-pos-printer copied to clipboard
image printing with "file://" scheme
Hello @tr3v3r ,
I'm trying to print an image file (in android) by passing a "file://" scheme uri as the uri property, like so:
const printing = new EscPosPrinter.printing();
const status = await printing
.initialize()
.align('center')
.image(
{uri: 'file:///data/user/0/com.example.reactnativeescposprinter/files/response.png'},
{ width: 576 }
)
.cut()
.send();
No error is thrown, but nothing is printed.
In the devices logcat logs I can see that BitmparFactory is throwing the following exception:
Unable to decode stream: java.io.FileNotFoundException: file:/data/user/0/com.example.reactnativeescposprinter/files/response.png: open failed: ENOENT (No such file or directory)
The file exists and if I convert it to base64 I am able to print it.
Can you please help?
Thanks!
@israelko have you figured out what's the problem?
@tr3v3r not yet. I will try to have a look early next week.
Hi @tr3v3r ,
I've found the issue: whereas the api documentation requires to pass a uri with a "file://" prefix for local storage file paths (and the android code depends on it so it will try to decode the file into bitmap), the BitmapFactory.decodeFile
method actually expects a uri without the prefix.
I think we can either remove the "file://" prefix from the path inside the android code before decoding the file or use another option prop for local file uri (instead of "uri" use "file" or something similar) to identify local file cases and don't require the "file:" prefix anymore.
What do you think?
BTW, in the iOS code there's no special handling for "file://" prefixed paths (and it's not documented as an android only feature). Are local file paths even supported? (I know very little about iOS development, so sorry if I'm missing something here...)
@israelko thanks a lot! But have you tried to print on iOS? The local file on iOS are supported and the path processes by function from RN itself.
For Android could you please share the code that works? I mean what I should send from js code to print image from file storage. I’ll collect all info from both platforms and create fix to support them
Sure. If we want to keep the current API, we should change the code in EscPosPrinterModule.java
to (prefixed added rows with "+" and removed rows with "-"):
if(uriString.startsWith("file")) {
+ String cleanUrl = uriString.replaceAll("file://", "");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
+ Bitmap image = BitmapFactory.decodeFile(cleanUrl, options);
- Bitmap image = BitmapFactory.decodeFile(uriString, options);
return image;
}
@israelko thanks! Will prepare new release with fix soon
Resolved in 4.0.0