I have an OCR android project that runs on Android Studio. When running the project, the phone opens, and asks user to take picture or select photo from gallery. After selecting the photo, thanks to crop libraries, I am cropping the photo to get desired words from the image. And when clicking detect text button on phone, it gets the word and prints on the screen. The main point is to use Firebase solution to hold detected words on database. I succesfully connected my app into Firebase Realtime Database. But the words are not written on db.
My mainActivity.java file is below;
package com.example.myapplication;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.vision.Frame;
import com.google.android.gms.vision.text.TextBlock;
import com.google.android.gms.vision.text.TextRecognizer;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.OAuthCredential;
import com.google.firebase.auth.OAuthProvider;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;
private void getTextFromImage(Bitmap bitmap) {
TextRecognizer recognizer = new TextRecognizer.Builder(this).build();
if (!recognizer.isOperational()) {
Toast.makeText(MainActivity.this, "Error Occurred!!", Toast.LENGTH_SHORT).show();
} else {
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
SparseArray<TextBlock> textBlockSparseArray = recognizer.detect(frame);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < textBlockSparseArray.size(); i++) {
textBlock = textBlockSparseArray.valueAt(i);
stringBuilder.append(textBlock.getValue());
stringBuilder.append("\n");
//System.out.println("yazı:"+textBlock.getValue());
}
textView_data.setText(stringBuilder.toString());
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
String uid = textBlock.getValue();
System.out.println("okutulan yazı 2: "+uid);
ReadWriteUserDetails writeUserDetails = new ReadWriteUserDetails(uid);
DatabaseReference referenceProfile = FirebaseDatabase.getInstance().getReference("Cropped all texts");
if (firebaseUser != null) {
referenceProfile = referenceProfile.child(firebaseUser.getUid());
}
System.out.println("hata verdi 2");
referenceProfile.child(firebaseUser.getUid()).setValue(writeUserDetails).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(MainActivity.this, "Data is written on the database successfully", Toast.LENGTH_LONG).show();
}
});
button_capture.setText("Retake");
button_copy.setVisibility(View.VISIBLE);
}
}
}
All codes related with database connection is under the getTextFromImage method. Therefore, it is better to focus inside the method, I think that there is a problem on referenceProblem or getUid(). There is no error on detecting text, the problem is to write detected text into database. Realtime Database connection is already done. When trying to run the app, taking the photo, cropping and detecting text on phone screen are all OK.
Here is the error I am getting on console;
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 28303
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=203, result=-1, data=Intent { launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 }(has extras) }} to activity {com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4520)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4563)
at android.app.ActivityThread.-wrap22(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1698)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at com.example.myapplication.MainActivity.getTextFromImage(MainActivity.java:144)
at com.example.myapplication.MainActivity.onActivityResult(MainActivity.java:104)
at android.app.Activity.dispatchActivityResult(Activity.java:7280)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4516)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4563)
at android.app.ActivityThread.-wrap22(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1698)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
I also tried to check nullability of the object. It would be really good, if you can help.