简体   繁体   English

Zxing Galaxy S3

[英]Zxing Galaxy S3

An application I'm building makes use of the zxing library to scan 1D and 2D barcodes. 我正在构建的应用程序利用zxing库扫描一维和二维条形码。 I am currently testing my app on a Galaxy S3 and each time I open the zxing Barcode Scanner app manually or through the IntentIntegrator library, the Barcode Scanner crashes after it succeeds in decoding the image. 我目前正在Galaxy S3上测试我的应用程序,每次我手动或通过IntentIntegrator库打开zxing Barcode Scanner应用程序时,Barcode Scanner在成功解码图像后都会崩溃。 I am unable to view a stack trace, as the error is occurring outside my project. 我无法查看堆栈跟踪,因为该错误发生在我的项目外部。

Has anyone been able to reproduce this error on their devices? 有人能够在其设备上重现此错误吗? Also is anyone aware of what causes the issue or what the fix is? 是否有人知道导致问题的原因或解决方法是什么?

If this isn't enough information, I can try downloading the source, building it myself, and identifying the issue from a stack trace. 如果这还不够,我可以尝试下载源代码,自己构建它,并从堆栈跟踪中识别问题。

Ok, so I downloaded the project source, built it, ran in on my device, and discovered the issue. 好的,所以我下载了项目源代码,进行了构建,在设备上运行后发现了问题。 The issue is related to the fact that the CaptureActivity puts information into the system's clipboard. 此问题与CaptureActivity将信息放入系统剪贴板有关。

In my experience while developing on a Galaxy S3, I've encountered multiple issues with Samsung's implementation of the Clipboard Service, namely the fact that requests to get the Clipboard Service return null. 根据我在Galaxy S3上进行开发的经验,我在三星实现剪贴板服务时遇到了多个问题,即获取剪贴板服务的请求返回null的事实。

Anyway, within ZXing's CaptureActivity, you find the following lines of code: 无论如何,在ZXing的CaptureActivity中,您会找到以下代码行:

if (copyToClipboard && !resultHandler.areContentsSecure()) {
  ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
  if (displayContents != null) {
    clipboard.setText(displayContents);
  }
}

Unfortunately, since the clipboard variable is null, the application will crash with a null pointer exception. 不幸的是,由于剪贴板变量为null,因此应用程序将崩溃,并出现null指针异常。 I commented out this part of the code, and the application seemed to run with no issues. 我注释掉了这部分代码,该应用程序似乎没有问题。 I believe that simply adding a null-check here should prevent issues on the S3 while allow the feature to remain on other devices. 我认为,仅在此处添加一个空检查就可以防止S3出现问题,同时允许该功能保留在其他设备上。 I am also assuming that other parts of the application do not depend on this data being entered into the clipboard; 我还假设应用程序的其他部分不依赖于输入到剪贴板中的数据。 however, as I am not familiar with all of the application's source code I cannot be sure of this. 但是,由于我不熟悉该应用程序的所有源代码,因此我不确定。

I will continue to attempt to integrate ZXing into my own project, and see if this fix works. 我将继续尝试将ZXing集成到我自己的项目中,并查看此修复程序是否有效。

Now if only someone (Samsung I assume) would fix the S3's implementation of the clipboard! 现在,如果只有一个人(我假设是三星)可以修复S3的剪贴板实现!

Update: 更新:

I tried a few different workarounds for this issue, including a simple null-check and the use of non-deprecated methods (zxing currently utilizes deprecated clipboard methods), and unfortunately none of those worked. 我针对此问题尝试了几种不同的解决方法,包括简单的null检查和使用不推荐使用的方法(zxing当前使用不推荐使用的剪贴板方法),但是不幸的是,这些方法均无效。 It appears that it is no the ClipboardManager itself that is null, but rather some member variable internal to it that is null. 似乎不是ClipboardManager本身为null,而是其内部的某些成员变量为null。 Once again, I'm guessing that this is specific to the S3, as this is the only phone I've encountered this issue on. 再一次,我猜这是S3专用的,因为这是我遇到此问题的唯一手机。

Unfortunately that means that the current best option for me is to remove the references to the clipboard entirely. 不幸的是,这意味着当前对我来说最好的选择是完全删除对剪贴板的引用。

Looking over my implementation in Barcode Scanner+ , I can suggest trying the newer clipboard API and trying this: 查看我在Barcode Scanner +中的实现 ,我建议尝试使用较新的剪贴板API并尝试执行以下操作:

ClipboardManager manager = ...;
manager.setPrimaryClip(ClipData.newPlainText(null, "your text here"));

This is only available if you can use the newer API in later Android verisons. 仅当您可以在以后的Android版本中使用较新的API时,此选项才可用。

Per my earlier comment, you can always catch NullPointerException and just continue if the copy is not critical. 根据我之前的评论,您始终可以捕获NullPointerException ,如果副本不是很关键,则可以继续。 I'll do that in one more place in the source for good measure. 我将在源代码中的另一位置进行适当的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM