0

我正在使用 Flutter 和 Brother Printer Native SDK 开发一个 Android 应用程序。我使用 MethodChannel 在 FlutterActivity 和 MainActivity 之间进行通信。这是我使用 Brother QL-810W 打印机打印标签的代码:

MainActivity.java

public class MainActivity extends FlutterActivity {

    private static final String CHANNEL = "brother/print";

    public static Printer printer;
    protected PrinterInfo settings;
    protected PrinterStatus bit;

    @Override
    public void configureFlutterEngine (@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        GeneratedPluginRegistrant.registerWith(flutterEngine);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
                .setMethodCallHandler(
                        (call, result) -> {

                            if (call.method.equals("print")) {

                                String address = call.argument("address");
                                String base64 = call.argument("base64");
                                int lblCount = 1; //call.argument("labels");

                                // Specify printer
                                printer = new Printer();
                                settings = new PrinterInfo();

                                settings = printer.getPrinterInfo();
                                settings.workPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/BrotherPrinter";
                                settings.printerModel = PrinterInfo.Model.QL_810W;
                                settings.port = PrinterInfo.Port.NET;
                                settings.ipAddress = address;

                                // Print Settings
                                settings.paperSize = PrinterInfo.PaperSize.CUSTOM;
                                settings.orientation = PrinterInfo.Orientation.LANDSCAPE;
                                settings.valign = PrinterInfo.VAlign.MIDDLE;
                                settings.align = PrinterInfo.Align.CENTER;
                                settings.printMode = PrinterInfo.PrintMode.FIT_TO_PAGE;
                                settings.numberOfCopies = lblCount;

                                //settings.labelNameIndex = LabelInfo.QL700.W62.ordinal();
                                settings.labelNameIndex = LabelInfo.QL700.W62RB.ordinal();

                                settings.isAutoCut = true;
                                settings.isCutAtEnd = false;
                                settings.isHalfCut = false;
                                settings.isSpecialTape = false;

                                printer.setPrinterInfo(settings);

                                if (printer.startCommunication()) {
                                    bit = new PrinterStatus();
                                    bit = printer.printImage(bmpFromBase64(base64));

                                    if (bit.errorCode != PrinterInfo.ErrorCode.ERROR_NONE) {
                                        Log.d("TAG", "ERROR - " + bit.errorCode);
                                        result.error("ERROR",bit.errorCode.toString(),"Error when Printing");

                                        Toast.makeText(this, "Error at printer: " + bit.errorCode.toString(), Toast.LENGTH_SHORT).show();
                                    }

                                    printer.endCommunication();
                                    result.success(0);
                                }

                            }else{
                                result.notImplemented();
                            }
                        });
    }

    public static Bitmap bmpFromBase64(String base64){
        try{
            byte[] bytes = Base64.decode(base64, Base64.DEFAULT);
            InputStream stream = new ByteArrayInputStream(bytes);

            return BitmapFactory.decodeStream(stream);
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

}

我在测试我的应用程序时收到以下错误。

Build fingerprint: 'Nokia/Onyx_00WW/B2N_sprout:10/QKQ1.190828.002/00WW_4_15N:user/release-keys'
Revision: '0'
ABI: 'arm64'
Timestamp: 2021-10-07 18:28:33+0200
pid: 18121, tid: 18121, name: fhs_date_labels  >>> com.abi_selvaraj.fhs_date_labels <<<
uid: 10241
signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr --------
Abort message: 'JNI DETECTED ERROR IN APPLICATION: GetStringUTFChars received NULL jstring
    in call to GetStringUTFChars
    from boolean com.brother.ptouch.sdk.JNIWrapper.writeByteInFile(int[], int, java.lang.String, boolean)'
    x0  0000000000000000  x1  00000000000046c9  x2  0000000000000006  x3  0000007fcd9c8270
    x4  fefeff7213592f97  x5  fefeff7213592f97  x6  fefeff7213592f97  x7  7f7f7f7f7f7f7fff
    x8  00000000000000f0  x9  dd7c75978bd06d95  x10 0000000000000001  x11 0000000000000000
    x12 fffffff0fffffbdf  x13 ffffffffffffffff  x14 0000000000000000  x15 ffffffffffffffff
    x16 0000007315fe78c0  x17 0000007315fc3900  x18 00000072172da558  x19 00000000000046c9
    x20 00000000000046c9  x21 00000000ffffffff  x22 00000072825f1760  x23 00000072926ad705
    x24 00000072926cf1f5  x25 0000000000000001  x26 00000072925bc258  x27 0000007318a06820
    x28 0000007292bd9338  x29 0000007fcd9c8310
    sp  0000007fcd9c8250  lr  0000007315f750c4  pc  0000007315f750f0
backtrace:
      #00 pc 00000000000830f0  /apex/com.android.runtime/lib64/bionic/libc.so (abort+160) (BuildId: cf739dbc84bcc78f7a1500721bfb3758)
      #01 pc 00000000004b99ac  /apex/com.android.runtime/lib64/libart.so (art::Runtime::Abort(char const*)+2280) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #02 pc 000000000000b458  /system/lib64/libbase.so (android::base::LogMessage::~LogMessage()+580) (BuildId: 36cd125456a5320dd3dcb8cfbd889a1a)
      #03 pc 0000000000377f60  /apex/com.android.runtime/lib64/libart.so (art::JavaVMExt::JniAbort(char const*, char const*)+1584) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #04 pc 0000000000378184  /apex/com.android.runtime/lib64/libart.so (art::JavaVMExt::JniAbortV(char const*, char const*, std::__va_list)+108) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #05 pc 000000000036a5ac  /apex/com.android.runtime/lib64/libart.so (art::(anonymous namespace)::ScopedCheck::AbortF(char const*, ...)+136) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #06 pc 0000000000369bfc  /apex/com.android.runtime/lib64/libart.so (art::(anonymous namespace)::ScopedCheck::CheckInstance(art::ScopedObjectAccess&, art::(anonymous namespace)::ScopedCheck::InstanceKind, _jobject*, bool)+420) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #07 pc 0000000000368e4c  /apex/com.android.runtime/lib64/libart.so (art::(anonymous namespace)::ScopedCheck::CheckPossibleHeapValue(art::ScopedObjectAccess&, char, art::(anonymous namespace)::JniValueType)+580) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #08 pc 0000000000368468  /apex/com.android.runtime/lib64/libart.so (art::(anonymous namespace)::ScopedCheck::Check(art::ScopedObjectAccess&, bool, char const*, art::(anonymous namespace)::JniValueType*)+652) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #09 pc 0000000000371560  /apex/com.android.runtime/lib64/libart.so (art::(anonymous namespace)::CheckJNI::GetStringCharsInternal(char const*, _JNIEnv*, _jstring*, unsigned char*, bool, bool)+676) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #10 pc 000000000003a198  /data/app/com.abi_selvaraj.fhs_date_labels-9UrlxSd-fzffTZQSnIcl_Q==/lib/arm64/libcreatedata.so (Java_com_brother_ptouch_sdk_JNIWrapper_writeByteInFile+108) (BuildId: f8707a015211e716396c82b381288ee73ac39880)
      #11 pc 0000000000140350  /apex/com.android.runtime/lib64/libart.so (art_quick_generic_jni_trampoline+144) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #12 pc 00000000001375b8  /apex/com.android.runtime/lib64/libart.so (art_quick_invoke_static_stub+568) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #13 pc 000000000014600c  /apex/com.android.runtime/lib64/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+276) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #14 pc 00000000002e3674  /apex/com.android.runtime/lib64/libart.so (art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, art::ShadowFrame*, unsigned short, art::JValue*)+384) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #15 pc 00000000002de8d4  /apex/com.android.runtime/lib64/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+892) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #16 pc 00000000005a3dd4  /apex/com.android.runtime/lib64/libart.so (MterpInvokeStatic+372) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #17 pc 0000000000131994  /apex/com.android.runtime/lib64/libart.so (mterp_op_invoke_static+20) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #18 pc 00000000001e2684  [anon:dalvik-classes2.dex extracted in memory from /data/app/com.abi_selvaraj.fhs_date_labels-9UrlxSd-fzffTZQSnIcl_Q==/base.apk!classes2.dex] (com.brother.ptouch.sdk.JNIWrapper.byteFileWriteRGB+176)
      #19 pc 00000000005a4070  /apex/com.android.runtime/lib64/libart.so (MterpInvokeStatic+1040) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #20 pc 0000000000131994  /apex/com.android.runtime/lib64/libart.so (mterp_op_invoke_static+20) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #21 pc 00000000001e2914  [anon:dalvik-classes2.dex extracted in memory from /data/app/com.abi_selvaraj.fhs_date_labels-9UrlxSd-fzffTZQSnIcl_Q==/base.apk!classes2.dex] (com.brother.ptouch.sdk.JNIWrapper.print+4)
      #22 pc 00000000005a4070  /apex/com.android.runtime/lib64/libart.so (MterpInvokeStatic+1040) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #23 pc 0000000000131994  /apex/com.android.runtime/lib64/libart.so (mterp_op_invoke_static+20) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #24 pc 00000000001ea6fc  [anon:dalvik-classes2.dex extracted in memory from /data/app/com.abi_selvaraj.fhs_date_labels-9UrlxSd-fzffTZQSnIcl_Q==/base.apk!classes2.dex] (com.brother.ptouch.sdk.Printer.printImage+196)
      #25 pc 00000000005a15c0  /apex/com.android.runtime/lib64/libart.so (MterpInvokeVirtual+1352) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #26 pc 0000000000131814  /apex/com.android.runtime/lib64/libart.so (mterp_op_invoke_virtual+20) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #27 pc 00000000001dd7e8  [anon:dalvik-classes2.dex extracted in memory from /data/app/com.abi_selvaraj.fhs_date_labels-9UrlxSd-fzffTZQSnIcl_Q==/base.apk!classes2.dex] (com.abi_selvaraj.fhs_date_labels.MainActivity.lambda$configureFlutterEngine$0$MainActivity+360)
      #28 pc 00000000005a15c0  /apex/com.android.runtime/lib64/libart.so (MterpInvokeVirtual+1352) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #29 pc 0000000000131814  /apex/com.android.runtime/lib64/libart.so (mterp_op_invoke_virtual+20) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #30 pc 00000000001dd55c  [anon:dalvik-classes2.dex extracted in memory from /data/app/com.abi_selvaraj.fhs_date_labels-9UrlxSd-fzffTZQSnIcl_Q==/base.apk!classes2.dex] (com.abi_selvaraj.fhs_date_labels.-$$Lambda$MainActivity$cnaSj6gUAqIaWfP_TCdFhmQeBEk.onMethodCall+4)
      #31 pc 00000000005a2de0  /apex/com.android.runtime/lib64/libart.so (MterpInvokeInterface+1788) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #32 pc 0000000000131a14  /apex/com.android.runtime/lib64/libart.so (mterp_op_invoke_interface+20) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #33 pc 0000000000156d0e  [anon:dalvik-classes3.dex extracted in memory from /data/app/com.abi_selvaraj.fhs_date_labels-9UrlxSd-fzffTZQSnIcl_Q==/base.apk!classes3.dex] (io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage+34)
      #34 pc 00000000005a2de0  /apex/com.android.runtime/lib64/libart.so (MterpInvokeInterface+1788) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #35 pc 0000000000131a14  /apex/com.android.runtime/lib64/libart.so (mterp_op_invoke_interface+20) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #36 pc 000000000014cce2  [anon:dalvik-classes3.dex extracted in memory from /data/app/com.abi_selvaraj.fhs_date_labels-9UrlxSd-fzffTZQSnIcl_Q==/base.apk!classes3.dex] (io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart+98)
      #37 pc 00000000005a2de0  /apex/com.android.runtime/lib64/libart.so (MterpInvokeInterface+1788) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #38 pc 0000000000131a14  /apex/com.android.runtime/lib64/libart.so (mterp_op_invoke_interface+20) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #39 pc 000000000014b908  [anon:dalvik-classes3.dex extracted in memory from /data/app/com.abi_selvaraj.fhs_date_labels-9UrlxSd-fzffTZQSnIcl_Q==/base.apk!classes3.dex] (io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage+8)
      #40 pc 00000000002b4988  /apex/com.android.runtime/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEbb.llvm.13098898000035350125+240) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #41 pc 0000000000592868  /apex/com.android.runtime/lib64/libart.so (artQuickToInterpreterBridge+1032) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #42 pc 0000000000140468  /apex/com.android.runtime/lib64/libart.so (art_quick_to_interpreter_bridge+88) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #43 pc 0000000000137334  /apex/com.android.runtime/lib64/libart.so (art_quick_invoke_stub+548) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #44 pc 0000000000145fec  /apex/com.android.runtime/lib64/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+244) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #45 pc 00000000004b126c  /apex/com.android.runtime/lib64/libart.so (art::(anonymous namespace)::InvokeWithArgArray(art::ScopedObjectAccessAlreadyRunnable const&, art::ArtMethod*, art::(anonymous namespace)::ArgArray*, art::JValue*, char const*)+104) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #46 pc 00000000004b26b8  /apex/com.android.runtime/lib64/libart.so (art::InvokeVirtualOrInterfaceWithVarArgs(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, _jmethodID*, std::__va_list)+424) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #47 pc 0000000000397f88  /apex/com.android.runtime/lib64/libart.so (art::JNI::CallVoidMethodV(_JNIEnv*, _jobject*, _jmethodID*, std::__va_list)+628) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #48 pc 000000000036ca20  /apex/com.android.runtime/lib64/libart.so (art::(anonymous namespace)::CheckJNI::CallMethodV(char const*, _JNIEnv*, _jobject*, _jclass*, _jmethodID*, std::__va_list, art::Primitive::Type, art::InvokeType)+2368) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #49 pc 000000000035ad00  /apex/com.android.runtime/lib64/libart.so (art::(anonymous namespace)::CheckJNI::CallVoidMethodV(_JNIEnv*, _jobject*, _jmethodID*, std::__va_list)+72) (BuildId: 9e1ab66d67be247f1f43b362accdaf63)
      #50 pc 00000000014628c8  /data/app/com.abi_selvaraj.fhs_date_labels-9UrlxSd-fzffTZQSnIcl_Q==/lib/arm64/libflutter.so (BuildId: fb1d4058f3179a6ac8718723ecfdf17c672f1abe)
      #51 pc 0000000040000000  <unknown>

我已经尝试了我在网上找到的关于 JNI Detected Error 的解决方案,它们都指向相同的答案:Include External Storage Permission in AndroidManifest.xml 是不够的,runtime permissions must be enabled for Android 6.0 and up。我也为我的设备启用了运行时权限。但错误似乎仍然存在。

请帮助我了解我可能出错的地方,以及我需要检查什么来解决这个问题。让我知道我是否应该再分享我身边的代码,我会这样做。因为这是在将我的应用程序分发给我的客户之前我需要解决的最后一件事。

编辑

错误与 Brother 打印机 SDK 的工作路径有关。我将其修改为以下内容,似乎已经解决了这个问题:

从:

settings.workPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/BrotherPrinter";

至:

settings.workPath = getFilesDir() + "/";
4

0 回答 0