my app makes use of ZXing barcode scanner using the ZXing helper classes IntentIntegrator and IntentResult.
Now I found there are no longer scanning results submitted from ZXing, the related return values are empty/null.
Thus I updated to latest helper classes http://code.google.com/p/zxing/source/browse/trunk/android-integration/src/com/google/zxing/integration/android/IntentIntegrator.java and http://code.google.com/p/zxing/source/browse/trunk/android-integration/src/com/google/zxing/integration/android/IntentResult.java
Now my onActivityResult method is called immediately after ZXing is started - of course with an empty result again.
My code is quite simple, scanning is started this way:
if (v==scanButton)
{
com.google.zxing.integration.android.IntentIntegrator integrator = new IntentIntegrator(this);
integrator.initiateScan();
}
and fetching the results this way:
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
com.google.zxing.integration.android.IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null)
{
String format;
format=scanResult.getFormatName();
if ((format!=null) && (format.length()>0))
{
if ((format.equals("EAN_8")) || (format.equals("EAN_13")) ||(format.equals("UPC_A")) ||(format.equals("UPC_E")))
getEANData(scanResult.getContents());
}
}
}
On my android the latest ZXing code is installed. Any ideas why it does not work any more?
From what I found out meanwhile: this seems to be an installation-dependent problem. On my Android device I can see that problem but it is not reproducible, other users of my App do not experience this. ZXing code itself wasn't changed for longer time and there users confirm it works too - so that seems to be a really ugly bug.
Here it happens with ZXing-code installed from Playstore, haven't tested it with ZXing included into my app yet...
Related
I have a "saved game chooser" activity for users to open saved games. I start the activity for result and return a string that represents the saved game to the parent activity.
The code I've been using is no more complex than this:
if(savedGameId != null) {
Intent result = new Intent();
result.putExtra("saved_game_key", savedGameId);
setResult(Activity.RESULT_OK, result);
finish();
}
In onActivityResult:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case SAVED_GAME_ACTIVITY:
if(resultCode == Activity.RESULT_OK) {
if(data.hasExtra("saved_game_key") {
String savedGameId = data.getExtra("saved_game_key");
...
} else {
// couldn't load game
}
}
break;
}
}
This has worked for me on all my test devices and emulators 100% of the time. However, I periodically get a crash report for a NPE for the data.hasExtra("saved_game_key") call.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.content.Intent.hasExtra(java.lang.String)' on a null object reference
at com.xxxx.yyyy.MainActivity.onActivityResult(MainActivity.java)
at android.app.Activity.dispatchActivityResult(Activity.java:6508)
...
The android versions and devices I've received this report so far:
Android 6.0 - MI MAX (hydrogen)
Android 6.0 - Lenovo K5 Note (A7020a48)
Android 6.0 - Moto G(4) (athene)
Android 5.1 - Galaxy Note 10.1 (lt033g)
Android 4.0.3 - 4.0.4 - Xperia tipo dual (ST21a2)
I have not been able to reproduce this issue myself. I'm going to try to get ahold of one of the above devices and see if I can get any more info.
In the meantime, I've searched google/stackoverflow and have not found anything that resembles this specific situation.
My Question
Is there something, some edge case that I've overlooked that could cause the returned data intent to be null in certain situations?
Like I said, I've never been able to reproduce this issue myself and the code looks correct based on other examples that I've seen for returning data from intents. If you need more information please leave me a comment.
I am developing a game for Android using Google Play Game Services, using Xamarin. I am doing my testing using a Genymotion Android Emulator. I have run into an issue that appears to be a bug in either Google Play or Xamarin's implementation.
If I sign out of a Google account, calls to the IGoogleApiClient.IsConnected() continue to return true (even though I have clearly just signed out). If I then attempt to use that API object, I will get exceptions like:
java.lang.SecurityException: Not signed in when calling API
For example, the follow code results in the above exception if executed after signing out:
public void StartNewMatch()
{
if (!mGoogleApiClient.IsConnected)
{
return;
}
Intent intent = GamesClass.TurnBasedMultiplayer.GetSelectOpponentsIntent(mGoogleApiClient, 1, 1, true);
StartActivityForResult(intent, RC_SELECT_PLAYERS);
}
I am signing out in the Google Play Games Inbox (match picker); as shown in the images below.
Anyone run into this before? Am I missing something? Got any work-arounds?
Note: This only occurs if signing out through Google's UI. If I manually sign the user out, with something like mGoogleApiClient.Disconnect(), the issue does not occur; mGoogleApiClient.IsConnected() now returns false (as expected).
In order to keep signed-in state synced up you MUST implement onActivityResult properly.
This should look something as follows:
NOTE: this is java code, I am not sure how this will look exactly using Xamarin, but hopefully you should be able to figure it out :)
#Override
protected void onActivityResult(int requestCode, int responseCode, Intent data) {
// check for "inconsistent state"
if ( responseCode == GamesActivityResultCodes.RESULT_RECONNECT_REQUIRED && requestCode == <your_request_code_here> ) {
// force a disconnect to sync up state, ensuring that mClient reports "not connected"
mGoogleApiClient.disconnect();
}
}
NOTE: just make sure to replace in the code with the request code you used. You may need to check for multiple request codes too.
If you are using gameHelper classes from BaseGameUtils library(it is easier to use), you may modify the above code to this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
gameHelper.onActivityResult(requestCode, resultCode, data);
if (resultCode == GamesActivityResultCodes.RESULT_RECONNECT_REQUIRED){
// force a disconnect to sync up state, ensuring that mClient reports "not connected"
gameHelper.getApiClient().disconnect();
}
}
I have android's Speech To Text API to speak something to the phone and convert it into text. By default, if one stops speaking to the microphone, the API assumes that the user is done talking and returns the text from the input speech.
For my application, the user might have long pauses between her consecutive sentences. How can I configure Android's speech to text API to consider the end of the speech only when I ask it to and not do that as soon as the speaker takes a small pause between sentences? Thanks!
Here is my current implementation which simply converts speech to text as soon as the user takes a small pause between sentences:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtText.setText(text.get(0));
}
break;
}
}
}
The API has 3 EXTRAs for that
EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS
EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS
EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS
But note that the API also says that "depending on the recognizer implementation, these values may have no effect", so you just have to test with the implementation that you are using if they have any effect or not. (I haven't done this test myself, so it would be great if you added a comment to this answer reporting your test results.)
Prior to Android 4.1 (or users of the Google Search/Now app) this will work for you:
int someValue = 5;
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, Long.valueOf(someValue * 1000L));
Unfortunately later versions no longer react to this parameter, a great shame as it makes lengthy note taking or email composing impossible....
I have brought the issue to their attention.
I have Zxing 1.7 and Android api 15. Integrated the scanner through Intent and added CaptureActivity so that application does not ask for which camera to use.Created core.jar and added it in Android project lib and added Zxing Android as library project to my project build path.
The scanner does not work and application crashes first time when i click on "san" tab of my app but next time the same functionality it works when i login back in my app and barcode can be scanned.
When the application crashes for first time it give ClassNot Found exception for CaptureActivity . I have followed all the requires steps and also added CaptureActivity in the Manifst.xml file.
Please advice as i have tried lots of alternatives. Does Zxing works only with Android API9 that is made obsolete now and its replacement is 10? Below is code for my ScanActivity
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Scan code
Intent intent= new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
//To aviod the complete action using dialog box
intent.setClassName(this, "com.google.zxing.client.android.CaptureActivity");
startActivityForResult( intent, 0 );
//getApplicationContext().startActivity( intent );
//View scanView = ((ActivityGroup) context).getLocalActivityManager().startActivity(id, newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
/*
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage( "com.google.zxing.client.android.SCAN" );
i.addCategory( Intent.CATEGORY_LAUNCHER );
startActivityForResult( i, 0);
*/
}
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
System.out.println("ScanRewardCodeActivity OnActivityResult Method");
if(requestCode == 0)
{
//Stop QR Code Reader intent
finishActivity( 0 );
TabActivity tabActivity = (TabActivity)getParent();
TabHost tabHost = tabActivity.getTabHost();
if(resultCode == RESULT_OK){
System.out.println("Done");
}
}
}
There are a number of problems here.
If you are using Intents, you do not need core/ or android/ code. Make sure to remove all of this code from your project as I think it is interfering and causing the crash.
You should not modify your AndroidManifest.xml
Yes zxing 1.7 works with all versions through to 4.0.x, but, you should use zxing 2.0 which was released a few days ago
Don't try to write your own integration code. Use the code given in android-integration/ as it definitely works
I am developing an Android application and I am currently having some troubles running Barcode Scanner (Zxing). I'm using Zxing as a "library project" in Eclipse.
I built Zxing core project with Ant, created my Zxing android project by importing sources in Eclipse and ticking the "Is Library" box. (That project uses the "core.jar" in its dependencies.)
Then I have my main project, which uses the Zxing library project, that project, uses the "core.jar" too.
So, here is the problem, when I run my application and start my Barcode Scanner Intent,
here is what I see on the screen :
http://imageshack.us/photo/my-images/52/screenbarcodescanner.png/
(I am sorry but I don't have enough reputation to post my screen here)
The strange thing is that it seems to recognize some things when I put my hand in front of the camera or some barcode, QR Code (it doesn't scan, but there are some green dots appearing on the red line, you know, it is a bit hard to aim the code without seeing anything on the screen ;) )
Finally, here is the way I am calling the Intent and managing the result, the basic way, as it is written on the Zxing Google code page :
#Override
public void onClick(View sender)
{
if(sender.equals(_scan_button))
{
startActivityForResult(new Intent("com.google.zxing.client.android.SCAN"), 0);
}
// ...
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
//String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// ...handle the result
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
I don't actually provide the code format to the Intent because I want it to scan any code format.
Last thing I can say, is that, before doing this with Zxing as a library project, I was using just as a "project dependency", that way, Zxing apk was installed on my phone if not installed, before running the main project. All was working very well, now it is embedded,...I'm stuck and I don't really why or what I am missing.
Thank you for your answers !
You're mixed up here. If you integrate by Intent, you do not need any code from Barcode Scanner. In the project, all you may need is the small bit of integration code you find in android-integration. This ought to solve your problem.