Joe Tan
Joe Tan

Reputation: 1

I am facing an issue with Android Studio Camera

I am using firebase text recognition to recognize text from camera images, however sometimes when I take a picture, I get an error message. I do not understand what the error is, I have already resized the image.


    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                photoURI = FileProvider.getUriForFile(this,
                        "com.ocr.test.ocr4.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            recreate();
        }
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

            imageBitmap = BitmapFactory.decodeFile(currentPhotoPath);



            //resize
            Bitmap resized = Bitmap.createScaledBitmap(imageBitmap, 500, 500, true);


                //added below
                FirebaseVisionImage firebaseVisionImage = FirebaseVisionImage.fromBitmap(resized);
                FirebaseVisionTextDetector firebaseVisionTextDetector = FirebaseVision.getInstance().getVisionTextDetector();
                firebaseVisionTextDetector.detectInImage(firebaseVisionImage).addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
                    @Override
                    public void onSuccess(FirebaseVisionText firebaseVisionText) {
                        getlocation();
                        createNewDiaglog(firebaseVisionText, resized);
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(ImagesActivity.this, "flop", Toast.LENGTH_SHORT).show();

                    }
                });
        }

    }

The Error message

java.lang.RuntimeException: Unable to resume activity {com.ocr.test.ocr4/com.ocr.test.ocr4.ImagesActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.ocr.test.ocr4/com.ocr.test.ocr4.ImagesActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4601) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4633) at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52) at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2187) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:236) at android.app.ActivityThread.main(ActivityThread.java:8057) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011) Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.ocr.test.ocr4/com.ocr.test.ocr4.ImagesActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference at android.app.ActivityThread.deliverResults(ActivityThread.java:5195) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4588) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4633)  at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)  at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2187)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:236)  at android.app.ActivityThread.main(ActivityThread.java:8057)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:800) at com.ocr.test.ocr4.ImagesActivity.onActivityResult(ImagesActivity.java:261) at android.app.Activity.dispatchActivityResult(Activity.java:8516) at android.app.ActivityThread.deliverResults(ActivityThread.java:5188) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4588)  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4633)  at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)  at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2187)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:236)  at android.app.ActivityThread.main(ActivityThread.java:8057)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)  2022-02-05 16:09:20.307 5031-5031/com.ocr.test.ocr4 I/Process: Sending signal. PID: 5031 SIG: 9

Upvotes: 0

Views: 376

Answers (0)

Related Questions