mardi 21 juin 2016

ZXing: Camera not reopening when app is reopened (MainActivity blank)?

Basically, what I am trying to do is switch activities (a history list), and be able to resume scanning. It switches activities, resumes the scanner view but no longer scans. When I close the app and reopen it, all I get us a blank white screen with the app name on top and a:

W/CameraBase: An error occurred while connecting to camera: 0

which I read occurs when the camera hasn't been closed properly. I can't seem to find where exactly the issue is. Been having issues with closing the camera properly for a few days now. I'm relatively new to Android Studio. I have looked around here and have been refraining from posting but nothing I found is working. My code is below. Any idea where my mistake might be? Thank you very much.

MainActivity.java

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.zxing.Result;

import java.io.IOException;
import java.io.OutputStreamWriter;
import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
    private ZXingScannerView mScannerView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mScannerView = new ZXingScannerView(this);   // Programmatically initialize the scanner view
        setContentView(mScannerView);

        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();         // Start camera
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    // Overflow menu with "History". Touching that button launches a new activity
    public boolean onOptionsItemSelected(MenuItem item) {
        int selectedId = item.getItemId();

        switch (selectedId) {
            case R.id.mniHistory:
                mScannerView.stopCamera();
                Intent historyIntent = new Intent(MainActivity.this, ResultsActivity.class);
                startActivity(historyIntent);

                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();           // Stop camera on pause

    }

    public void onResume() {
        super.onResume();
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();          // Start camera on resume
        mScannerView.resumeCameraPreview(MainActivity.this);
    }

    @Override
    public void handleResult(final Result rawResult) {
        // Do something with the result here


        Log.e("handler", rawResult.getText()); // Prints scan results
        Log.e("handler", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode)


        // Alert Box
        AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
        builder1.setTitle("Scan Result");
        builder1.setMessage(rawResult.getText() + "n" + "Would you like to send this?");
        builder1.setCancelable(true);

        builder1.setPositiveButton(
                "Yes",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Toast.makeText(MainActivity.this, "Message Sent", Toast.LENGTH_LONG).show();


                        // Add scan to text file
                        try {
                            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("History.txt", Context.MODE_APPEND));
                            outputStreamWriter.write(rawResult.getText());
                            outputStreamWriter.append("n");
                            outputStreamWriter.close();
                        } catch (IOException e) {
                            Log.e("Exception", "File write failed: " + e.toString());
                        }

                        dialog.cancel();
                    }
                });

        builder1.setNegativeButton(
                "No",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                        mScannerView.resumeCameraPreview(MainActivity.this);
                    }
                });

        AlertDialog alert11 = builder1.create();
        alert11.show();
    }
}

Aucun commentaire:

Enregistrer un commentaire