I'm having some issue with zxing onActivityResult().
As you can see in the code I did properly invoke new intent as described in https://code.google.com/p/zxing/wiki/ScanningViaIntent.
The question is how can I catch onActivityResult() in Fragment, since I need this data in my Fragmnet and not in Activity?
package com.example.testingcodereading;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MainFragment extends Fragment {
private Button mButtonXZing;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_main, parent, false);
mButtonXZing = (Button) v.findViewById(R.id.button_xzing);
mButtonXZing.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(getActivity());
integrator.initiateScan();
}
});
return v;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
System.out.println("never here");
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
// handle scan result
}
// else continue with any other code you need in the method
}
}
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
Fragment f = fm.findFragmentById(R.id.fragmentContainer);
if(f == null){
f = new MainFragment();
fm.beginTransaction()
.add(R.id.fragmentContainer, f)
.commit();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
System.out.println("the code is catch");
}
}
As Martynnw pointed out the issue is to call fragment.startActivityForResult instead of activity.startActivityForResult. So just use next wrapper:
import android.content.Intent;
import android.support.v4.app.Fragment;
import com.google.zxing.integration.android.IntentIntegrator;
public final class FragmentIntentIntegrator extends IntentIntegrator {
private final Fragment fragment;
public FragmentIntentIntegrator(Fragment fragment) {
super(fragment.getActivity());
this.fragment = fragment;
}
#Override
protected void startActivityForResult(Intent intent, int code) {
fragment.startActivityForResult(intent, code);
}
}
integrator.initiateScan();
Change the above line as
integrator.forSupportFragment(fragment_name.this).initiateScan();
Alternative ZXing Android Embedded, implementation:
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new IntentIntegrator(this).initiateScan(); // `this` is the current Activity
}
Use from a Fragment:
IntentIntegrator.forFragment(this).initiateScan(); // `this` is the current Fragment
// If you're using the support library, use:
// IntentIntegrator.forSupportFragment(this) instead.
Get the results:
#Override
public void onActivityResult(int requestCode, int resultCode,Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
more info and options
If any one have the same issue here is my solution.
package com.example.testingcodereading;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fm = getSupportFragmentManager();
Fragment f = fm.findFragmentById(R.id.fragmentContainer);
if (f == null) {
f = MainFragment.newInstance("Start Application");
fm.beginTransaction().add(R.id.fragmentContainer, f).commit();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
System.out.println("the code is catch");
IntentResult scanResult = IntentIntegrator.parseActivityResult(
requestCode, resultCode, intent);
// handle scan result
if (scanResult != null) {
FragmentManager fm = getSupportFragmentManager();
Fragment newFrame = MainFragment.newInstance(scanResult.toString());
fm.beginTransaction().replace(R.id.fragmentContainer, newFrame).commit();
}
}
}
package com.example.testingcodereading;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.test.suitebuilder.annotation.MediumTest;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainFragment extends Fragment {
private static final String EXTRA_CODE = "com.example.testingcodereading.code";
private Button mButtonXZing;
private TextView mTextView;
public static MainFragment newInstance(String code) {
Bundle args = new Bundle();
args.putSerializable(EXTRA_CODE, code);
MainFragment fragment = new MainFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_main, parent, false);
mTextView = (TextView) v.findViewById(R.id.text_code);
mTextView.setText((String) getArguments().getSerializable(EXTRA_CODE));
mButtonXZing = (Button) v.findViewById(R.id.button_xzing);
mButtonXZing.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(getActivity());
integrator.initiateScan();
}
});
return v;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
System.out.println("never here");
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
// handle scan result
}
// else continue with any other code you need in the method
}
}
I had this problem and resolved it with below codes:
IntentIntegrator integrator = new IntentIntegrator(getActivity());
integrator.forSupportFragment(MyFragment.this).initiateScan();
Shall call the scan in this way:
IntentIntegrator.forSupportFragment(YourFragment.this)
.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE)
.setBeepEnabled(true/false)
.setPrompt("blahblahblah")
.initiateScan();
Then you can get the scan result in Fragment's onActivityResult
Make sure OnActivityResult in your Activity is calling super.OnActivityResult(). That should ensure it's called on the Fragment as well.
Alternatively, you could modify the IntentIntegrator code so it calls StartActivityResult on the Fragment, either by passing the fragment to the constructor, or passing it to initiateScan.
Try creating IntentIntegrator object in Fragment as below.
val scanIntegrator = IntentIntegrator.forSupportFragment(this#HomeFragment)
scanIntegrator.setPrompt("Scan")
scanIntegrator.setBeepEnabled(true)
//The following line if you want QR code
scanIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES)
scanIntegrator.captureActivity = CaptureActivityAnyOrientation::class.java
scanIntegrator.setOrientationLocked(true)
scanIntegrator.setBarcodeImageEnabled(true)
scanIntegrator.initiateScan()
public void scanCode() {
IntentIntegrator.forSupportFragment(this).
setCaptureActivity(CaptureAct.class).
setOrientationLocked(false)
.setDesiredBarcodeFormats(IntentIntegrator.PRODUCT_CODE_TYPES)
.setPrompt("Scanning code")
.initiateScan();
}
A solution : A basic example of how you can manage it
Main Activity
public static int tapTrick = 0;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
if(tapTrick!=0 && tapTrick==1) {
Tab1.onActivityResult(requestCode, resultCode, data, MainActivity.this);
}else if (tapTrick!=0 && tapTrick==2){
//Tab2.onActivityResult(requestCode, resultCode, data, MainActivity.this);
}
}
Fragment 1
MainActivity.tapTrick=1; // onCreateView or onClick (load images/files)
public static void onActivityResult(int requestCode, int resultCode, Intent intent, Context context) {
}
Fragment 2
MainActivity.tapTrick=2;
public static void onActivityResult(int requestCode, int resultCode, Intent intent, Context context) {
}
Fragment x
MainActivity.tapTrick=x;
public static void onActivityResult(int requestCode, int resultCode, Intent intent, Context context) {
}
Related
I have a MainAcitivity and over it I have fragment A which contains view pager with two fragments B and C.....Frgamnet B is at 0 index of viewpager in fragment A and contains profile section to upload a profile picture from gallery or camera. When a picture is selcted , onActivity result of main activity is called and then I have redirected it to instance of viewpager frgamentA but my problem is how to call onActivityResult of fragment B where main display picture is to be set in an imageview??
MainAcitivity -> frgamentA -> Fragment B
in anticipation of a positive reply..
FRAGMENTB.java......
private void galleryIntent() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
check="file";
startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (check.equalsIgnoreCase("file"))
onSelectFromGalleryResult(data);
else if (check.equalsIgnoreCase("camera"))
onCaptureImageResult(data);
}
}
MAIN ACTIVITY.java.
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Fragment f = getSupportFragmentManager().findFragmentById(R.id.frame);
if (f instanceof FragmentA) { //fragement A is viewpager fragment
f.onActivityResult(requestCode, resultCode, intent);
}
}
FRAGMENTA.java............
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
getTargetFragment().onActivityResult(requestCode, resultCode, intent);
}
HI need to call onActivityResult of FragmentB ??
You have to call startActivityForResult() from component which must handle the activityResult.
For example:
if you want to handle the result in Activity myAwesomeActivity:
myAwesomeActivity.startActivityForResult();
if you want to handle the result in Fragment myAwesomeFragment
myAwesomeFragment.startActivityForResult();
If you want call startActivityForResult in fragment you 'll use getActivity() first.
private void galleryIntent() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
check="file";
getActivity().startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
}
You need to call startActivityForResult with request Code
I have created an example for it's working fine try it.
Activity File
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
public class ViewPagerActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewpager);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frameContainer, new FragmentA(), "ft");
transaction.addToBackStack(null);
transaction.commit();
}
}
Fragment A
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.Objects;
public class FragmentA extends Fragment {
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag_a, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewPager = view.findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(Objects.requireNonNull(getActivity()).getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
}
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private int NUM_ITEMS = 2;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 0: // Fragment # 0 - This will show FirstFragment
return new FragmentB();
case 1: // Fragment # 0 - This will show FirstFragment different title
return new FragmentC();
default:
return null;
}
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Frag B";
case 1:
return "Frag C";
}
return "Page " + position;
}
}
}
FragmentB
public class FragmentB extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag_b, container, false);
}
}
And Here is FragmentC where you call startActivityForResult
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class FragmentC extends Fragment {
Button btnSelect;
int CAMERA_CODE = 1001;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag_c, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
btnSelect = view.findViewById(R.id.btnSelect);
btnSelect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraI = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraI, CAMERA_CODE);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_CODE) {
Log.e("got it", "Frag C Got it.");
}
}
}
For this, you'd better call
getParentFragment().startActivityForResult(),
and in that parent fragment, then, you can implement
onActivityResult()
and in that method deliver the result to the child fragmen,
childFragment.getParentFragment().onActivityResult(requestCode, resultCode, data)
I have a simple QR code scanning app. Here's the flowchart which describes its logic.
And this is the main part:
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class ScanActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
}
#Override
protected void onResume() {
super.onResume();
IntentIntegrator scanIntegrator = new IntentIntegrator(ScanActivity.this);
scanIntegrator.setOrientationLocked(false);
scanIntegrator.initiateScan();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanResult != null){
String strResult = scanResult.getContents();
Intent iii = new Intent(ScanActivity.this, ScanResultActivity.class);
iii.putExtra("scan_result", strResult);
startActivity(iii);
}
}
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
}
If you are in ScanActivity, then the app will immediately display ScanResultActivity after scanning, which is as expected. The only issue is app won't quit when Back key is pressed. Instead, it will jump to ScanResultActivity. Of course, the scanning result is null.
How to quit the app by pressing Back key when scanning is running?
Full code: https://github.com/anta40/QRScanDemo
this method
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
should call the super method, like this
#Override
public void onBackPressed() {
super.onBackPressed();
}
i am using voice recognizer in my app. but i want a single result from the voice recognizer without the "[" and "]" in the beginning and the end of the result provided by the voice recognizer.
at present i have a code which gives me a single result but it give "[" and "]" in the front and in the end of the result which i obtain.
please check my code make the possible correction and modifications and give a appropiate answer i am very new to android.
code : MainActivity.java
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final int RECOGNIZER_EXAMPLE = 1001;
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.text_result);
//set up button listner
Button startButton = (Button) findViewById(R.id.trigger);
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent =
new Intent (RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"SAY A WORD OR PHRASE\nAND IT WILL BE SHOWN AS TEXT");
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
startActivityForResult(intent,RECOGNIZER_EXAMPLE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//use a switch statament for more than one request code check
if(requestCode==RECOGNIZER_EXAMPLE && resultCode==RESULT_OK) {
//RETURNED DATA IS A LIST OF MATCHES TO THE SPEECH IPUT
ArrayList<String> result =
data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
tv.setText(result.toString());
}
super.onActivityResult(requestCode, resultCode, data);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//use a switch statament for more than one request code check
if(requestCode==RECOGNIZER_EXAMPLE && resultCode==RESULT_OK) {
//RETURNED DATA IS A LIST OF MATCHES TO THE SPEECH IPUT
ArrayList<String> result =
data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
// result here is array list , we need any element to be viewed in textview
tv.setText(result.get(0).toString());
}
super.onActivityResult(requestCode, resultCode, data);
}
So the change we made
tv.setText(result.toString());
To
tv.setText(result.get(0).toString());
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final int RECOGNIZER_EXAMPLE = 1001;
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.text_result);
//set up button listner
Button startButton = (Button) findViewById(R.id.trigger);
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent =
new Intent (RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"SAY A WORD OR PHRASE\nAND IT WILL BE SHOWN AS TEXT");
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
startActivityForResult(intent,RECOGNIZER_EXAMPLE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//use a switch statament for more than one request code check
if(requestCode==RECOGNIZER_EXAMPLE && resultCode==RESULT_OK) {
//RETURNED DATA IS A LIST OF MATCHES TO THE SPEECH IPUT
ArrayList<String> result =
data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
tv.setText(result.get(0).toString());
}
super.onActivityResult(requestCode, resultCode, data);
}
}
i'm trying to integrate Zxing library, and use the barcode scanner from my app.
so, downloaded the 2 java files IntentIntegrator and IntentResult, put them into this package:
com.google.zxing.integration
where my app is in this package:
com.example.mindstormsgamepad
the code I'm using in my activity is:
package com.example.mindstormsgamepad;
import com.google.zxing.integration.IntentIntegrator;
import com.google.zxing.integration.IntentResult;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.Button;
import android.widget.TextView;
/**
* BarcodeScan Activity
*/
public class BarcodeScanActivity extends CommonActivity implements OnClickListener{
/** Debug */
protected String TAG_SUB = "BarcodeScanActivity";
private Button scanBtn;
private TextView formatTxt, contentTxt;
#Override
public void onCreate(Bundle savedInstanceState) {
initTabSub( TAG_SUB );
log_d( "onCreate" );
super.onCreate( savedInstanceState );
/* set the layout on the screen */
View view = getLayoutInflater().inflate( R.layout.activity_barcode_scan, null );
setContentView( view );
/* Initialization of Bluetooth */
initManager( view );
setTitleName( R.string.activity_barcodescan );
initButtonBack();
initInputDeviceManager();
Toast.makeText(BarcodeScanActivity.this, "QR Scan!", Toast.LENGTH_SHORT).show();
/* Initialization of Scanning */
scanBtn = (Button)findViewById(R.id.scan_button);
formatTxt = (TextView)findViewById(R.id.scan_format);
contentTxt = (TextView)findViewById(R.id.scan_content);
scanBtn.setOnClickListener(this);
}
// --- onCreate end ---
/**
* === onResume ===
*/
#Override
public void onResume() {
log_d( "onResume()" );
super.onResume();
startService();
mInputDeviceManager.register();
}
/**
* === onPause ===
*/
#Override
public void onPause() {
log_d( "onPause()" );
super.onPause();
sendStop();
mInputDeviceManager.unregister();
}
#Override
public void onClick(View v) {
// Start Scan
if(v.getId()==R.id.scan_button){
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
//retrieve scan result
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
//we have a result
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
formatTxt.setText("FORMAT: " + scanFormat);
contentTxt.setText("CONTENT: " + scanContent);
}else{
Toast toast = Toast.makeText(getApplicationContext(),
"No Barcode data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
}
but I'm getting this error,
The constructor IntentIntegrator(BarcodeScanActivity) is undefined
and
The method initiateScan(Activity) in the type IntentIntegrator is not applicable for the arguments ()
on these lines:
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
I'm new to android programming,
how to solve this problem?
thanks for your help.
Try This :
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE,PRODUCT_MODE");
startActivityForResult(intent, 0);
For Result Do the code in onActivityResult method
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Toast.makeText(getActivity(), "result ", 1000).show();
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
//do code here
}
else if (resultCode == RESULT_CANCELED) {
//do code here
}
}
This means your Activity does not ultimately extend android.app.Activity. see what CommonActivity extends. Otherwise maybe you somehow have an old or wrong copy of the IntentIntegrator.
I am integrating the zxing scanner into my android application and I am coming across a weird situation where the onActivityResult (in the activity initiating the scan) is being called before the scanning intent opens. I have looked at several examples and my code seems to match what I am seeing in many of the tutorials. Here is the code for the activity.
package com.honeydewit;
import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.honeydewit.adapters.ListItemAdapter;
import com.honeydewit.listeners.OneOffClickListener;
import com.honeydewit.pojos.BasicList;
import com.honeydewit.pojos.ListItem;
public class ListHomeActivity extends BasicActivity{
private ImageButton addItemBtn;
private ImageButton addByScanBtn;
private ArrayList<ListItem> lists = new ArrayList<ListItem>();
public static ListItemAdapter listAdapter;
private TextView headerTxt;
private BasicList basicList;
private ListView listView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listshome);
listView =(ListView) findViewById(R.id.list);
basicList = getApplicationContext().getCurrentList();
headerTxt = (TextView)findViewById(R.id.headerTxt);
headerTxt.setText(basicList.getName());
headerTxt.setTypeface(getApplicationContext().getTypeface());
//add button
addItemBtn = (ImageButton)findViewById(R.id.add);
addItemBtn.setOnClickListener(new OneOffClickListener() {
#Override
public void onClick(View v) {
addToList(v, basicList);
}
});
addByScanBtn = (ImageButton)findViewById(R.id.addByScan);
addByScanBtn.setVisibility(View.VISIBLE);
addByScanBtn.setOnClickListener(new OneOffClickListener() {
#Override
public void onClick(View v) {
addToListByScan(v, basicList);
}
});
setupListAdapter(basicList.get_id());
}
private void setupListAdapter(int listId) {
populateListItems(listId);
listAdapter = new ListItemAdapter(this, R.layout.listrow, lists);
listView.setAdapter(listAdapter);
}
private void populateListItems(int listId) {
ArrayList<ListItem> items = (ArrayList<ListItem>)getApplicationContext().getShoppingListDbHelper().getShoppingListItems(listId);
for(ListItem item : items ) {
lists.add(item);
}
}
private void addToList(View view, BasicList list) {
if(basicList.getListTypeId() == Constants.TODO_LIST_TYPE_CDE) {
Intent newListIntent = new Intent(getBaseContext(), ToDoItemActivity.class);
startActivityForResult(newListIntent, 1);
}
else {
Intent newListIntent = new Intent(getBaseContext(), ItemActivity.class);
startActivityForResult(newListIntent, 1);
}
}
private void addToListByScan(View view, BasicList list) {
try {
IntentIntegrator zxingIntegrator = new IntentIntegrator(this);
zxingIntegrator.initiateScan();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "ERROR:" + e, 1).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
Log.d(getClass().getName(), scanResult.getContents());
Log.d(getClass().getName(), scanResult.getFormatName());
Log.d(getClass().getName(),data.getStringExtra("SCAN_RESULT_FORMAT"));
Log.d(getClass().getName(),data.getStringExtra("SCAN_RESULT"));
}
}
}
}
onActivityResult is called by Android whenever your app needs to be told about an Intent. That could be from lots of places. You know by looking at the request code and comparing it to the one in IntentIntegrator. Or let that class do all this for you.
My guess is your intent-filter is too broad and you're hearing things you don't expect.