Zxing Bar code scanner issue in Android

474 Views Asked by At

I am working on a barcode scanner App where on button click in the first Activity, I am moving to the BarcodeScanner Activity where I am importing Zxing library functionalities. Once the scanning is completed, I am moving to a 3rd Activity where I am showing the scanned Results. On clicking a button in the 3rd activity, i am coming back to the 1st activity. For devices having Marshmallow, the code is running fine. But the issue is happening with devices having versions below marshmallow where after going back to the 1st activity from the 3rd Activity, when i am pressing again the button, the scanner activity is appearing but the camera is not starting. It just showing a blank page. Please help. Below I am posting my codes for all 3 Activities.

First Activity:

        public class FirstActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_first);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                getWindow().setStatusBarColor(Color.parseColor("#FDB50A"));
            }
            ImageView Scan= (ImageView) findViewById(R.id.scanButton);
            Scan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FirstActivity.this.finish();
                    Intent nextPage= new Intent(FirstActivity.this,MainActivity.class);
                    startActivity(nextPage);


                }
            });


ScannerActivity:

    public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{

        Integer response = 0 ;
        int currentIndex=0;
        Boolean flash=false;
        DataBaseHelper dataBaseHelper;

private ZXingScannerView mScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.e("onCreate", "onCreate");
        checkPermissions();
        mScannerView = new ZXingScannerView(this);
        mScannerView.setResultHandler(this);
boolean cam= isCameraUsebyApp();
        Log.d("cameraBar",cam+"");
        if(cam)
        {
                mScannerView.stopCamera();
        }
        cam= isCameraUsebyApp();
        Log.d("cameraBar",cam+"");

        mScannerView.startCamera();
//        FrameLayout frameLayout= new FrameLayout(this);
//        FrameLayout.LayoutParams mainParam= new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
//        frameLayout.setLayoutParams(mainParam);
//        Button scanButton= new Button(this);
        dataBaseHelper= new DataBaseHelper(this);
        if(dataBaseHelper.checkDataBase()==false)
        {
                try {
                        dataBaseHelper.createDataBase();
                } catch (IOException e)
                {
                        e.printStackTrace();
                }
        }
        else{

        }

        Log.d("AnimeshSQL","copy");

        dataBaseHelper.openDataBase();
//        List<String> data=dataBaseHelper.getQuotes("n",1);
     //   Log.d("AnimeshSQL",data.get(0).toString());
        LayoutParams params =
                new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
//        scanButton.setBackground(getResources().getDrawable(R.drawable.round_button));
//        scanButton.setText("Flash");
//        scanButton.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                     if(flash==false)
//                     {
//                             flash=true;
//
//
//                     }
//                        else
//                     {
//                           flash=false;
//                     }
//                        mScannerView.setFlash(flash);
//                }
//        });
//        scanButton.setLayoutParams(params);
//        frameLayout.addView(mScannerView);
//        frameLayout.addView(scanButton);
       // setContentView(mScannerView);
        checkPermissions();
        if(response == 1) {
                mScannerView = null;
                mScannerView = new ZXingScannerView(this);
                setContentView(mScannerView);
                response = 0;

        }



}
        public boolean isCameraUsebyApp() {
                Camera camera = null;
                try {
                        camera = Camera.open();
                } catch (RuntimeException e) {
                        return true;
                } finally {
                        if (camera != null) camera.release();
                }
                return false;
        }
        private void checkPermissions() {
                try {
                        for (int i = currentIndex; i < permissions.length; i++) {
                                currentIndex = currentIndex + 1;
                                int result = ContextCompat.checkSelfPermission(context, permissions[i]);
                                if (result == PackageManager.PERMISSION_GRANTED) {
                                } else {
                                        requestPermission(permissions[i]);
                                        return;
                                }
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        Activity activity = this;
        Context context = this;

        String[] permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};


        private void requestPermission(String permission) {

//
                ActivityCompat.requestPermissions(activity, new String[]{permission}, 101);
//
        }

        @Override
        public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
                switch (requestCode) {
                        case 101:
                                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//
                                        checkPermissions();
                                } else {
                                        try {
                                                // FuncUtils.showToast(context, permissions[0] + " Denied!!!");
                                        } catch (Exception e) {
                                                e.printStackTrace();
                                        }
//
///
                                }
                                break;
                }
        }


        @Override
public void onResume() {
        super.onResume();

        if(response == 1) {
        mScannerView = null;
        mScannerView = new ZXingScannerView(this);
        setContentView(mScannerView);
        response = 0;

        }

        mScannerView.setResultHandler(this);
        mScannerView.startCamera();

        }

@Override
public void onDestroy() {
        super.onDestroy();


        mScannerView.stopCamera();

        }

        @Override
        protected void onRestart() {
                super.onRestart();
                Log.d("ani","onrestart");
        }

        @Override
public void handleResult(Result rawResult)
        {
               //Some codes to handle the result
  Intent intent= new Intent(this,ScanResultActivity.class);
                startActivity(intent);
                //vbn
                mScannerView.stopCamera();
                MainActivity.this.finish();

         }
}


Final Activity:


    public class ScanResultActivity extends AppCompatActivity {
    SharedPreferences prefs;
    Button ok;
    ImageView Hubbell,CI;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_result);
        prefs = getSharedPreferences("ScanPref", MODE_PRIVATE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().setStatusBarColor(Color.parseColor("#FDB50A"));
        }
               //Codes to show the data
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ScanResultActivity.this.finish();
                Intent nextPage= new Intent(ScanResultActivity.this,FirstActivity.class);
                startActivity(nextPage);

            }
        });
1

There are 1 best solutions below

0
On

You can write Intent in OnActivityResult.

 // Call Back method  to get the Message form other Activity  
    @Override  
       protected void onActivityResult(int requestCode, int resultCode, Intent data)  
       {  
                 super.onActivityResult(requestCode, resultCode, data);  
                  // check if the request code is same as what is passed  here it is 2  
                   if(requestCode==2)  
                         {  
                            String message=data.getStringExtra("MESSAGE");   
                            textView1.setText(message);  
                         }  
     }