Android barcode, onActivityResult not calling

流过昼夜 提交于 2020-11-25 03:56:48

问题


I am using Zxing barcode scan. Like most of the issues I am facing the same one. That It's not hitting the onActivityResult. I am using this tutorial

Layout

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:padding="2dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left|center_vertical"

                android:text="Smart MSN #" />

            <AutoCompleteTextView
                android:id="@+id/smart_msn_spinner"
                android:layout_width="157dp"
                android:layout_height="wrap_content"
                android:layout_gravity="left|center_vertical"
                android:inputType="number" />

            <ImageButton
                android:id="@+id/start_scan"
                android:layout_width="66dp"
                android:layout_height="match_parent"
                android:background="@color/white"
                app:srcCompat="@drawable/ic_baseline_qr_code_scanner_24" />

        </LinearLayout>

Fragment

public class NewFormFragment extends Fragment {

  @BindView(R.id.start_scan)
  ImageButton startScan;
  
  @BindView(R.id.smart_msn_spinner)
  AutoCompleteTextView smartMsnSpinner;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   bindListners();
  }

 private void bindListners() {
   .
   .
   .
   .
   startScan.setOnClickListener(startScanning);
   }

private final View.OnClickListener startScanning = v -> {
    if(v.getId()==R.id.start_scan){
        IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity());
        scanIntegrator.initiateScan();
    }
    };

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (scanningResult != null) {
        String scanContent = scanningResult.getContents();
        String scanFormat = scanningResult.getFormatName();
        //smartMsnSpinner.setText("FORMAT: " + scanFormat);
        smartMsnSpinner.setText(scanContent);
    }
    else{
        Toast toast = Toast.makeText(getActivity(),
                "No scan data received!", Toast.LENGTH_SHORT);
        toast.show();
    }

//Below part is working fine for taking snaps


    if ((requestCode == Constants.REQUEST_TAKE_PHOTO_SITE || requestCode == Constants.REQUEST_TAKE_PHOTO_DOCS) && resultCode == Activity.RESULT_OK) {
        Bitmap photo;
        if (data != null) {
            photo = (Bitmap) data.getExtras().get("data");
        } else {

            String compressPath = ImageResize.compressImage(getActivity(), latestSelectedImagePath);

            try {
                photo = ImageResize.loadScaledBitmap(getActivity(), compressPath);
            } catch (FileNotFoundException e) {
                photo = null;
            }
            if (requestCode == Constants.REQUEST_TAKE_PHOTO_SITE) {
                siteImagesCount++;
                siteImagesNames.add(compressPath);
                siteImagesSliderAdapter.adBitmap(photo);
                siteSnapsRecyclerView.getLayoutManager().scrollToPosition(siteSliderLinearLayoutManager.findLastVisibleItemPosition() + 1);
            } else {
                docsImageCount++;
                docImagesNames.add(compressPath);
                docsImagesSliderAdapter.adBitmap(photo);
                docSnapsRecyclerView.getLayoutManager().scrollToPosition(docSliderLinearLayoutManager.findLastVisibleItemPosition() + 1);
            }
            mScrollView.fullScroll(View.FOCUS_DOWN);
            mScrollView.fullScroll(View.FOCUS_DOWN);

        }

    } else if (requestCode == Constants.REQUEST_LOCATION_SETTINGS) {
        if (checkLocationPermission()) {
            if (gpsTracker != null) {
                if (gpsTracker.canGetLocation()) {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            //Do something after 100ms
                            getGPSLocation();
                        }
                    }, 1000);
                }
            }
        }
    }
}

I am able to scan the barcode but after the barcode scan it never hits the onActivityResult.

Any help?

来源:https://stackoverflow.com/questions/64784906/android-barcode-onactivityresult-not-calling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!