Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException after Image Capture #46

Open
Syed-Affan-Hameed opened this issue Mar 7, 2023 · 3 comments
Open

NullPointerException after Image Capture #46

Syed-Affan-Hameed opened this issue Mar 7, 2023 · 3 comments

Comments

@Syed-Affan-Hameed
Copy link

Below is the Error:
" java.lang.RuntimeException: Unable to start activity ComponentInfo{com.labters.documentscannerandroid/com.labters.documentscannerandroid.ImageCropActivity}: java.lang.NullPointerException: contentResolver!!.openIn…odeStream(this)
} must not be null ".
The application works fine when I select the image from the gallery. But when I use camera to capture, the above error comes up in the console.
is this a known issue?, can anyone help me resolve this?
Thanks

@KamranKhanDemo
Copy link

All I can guess , it is image path error, you need to double check it is not happening to me either.

@KamranKhanDemo
Copy link

The follwoing code I have written in Java for ImageCropActivity, you can use that if you are using Java, or better to use Already written Kotlin Code
`import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.your_project.R;

import com.labters.documentscanner.DocumentScannerView;

import kotlin.Unit;

public class ImageCropActivity extends AppCompatActivity {

private static final String FILE_DIR = "FileDir";
DocumentScannerView documentScannerView;

ImageView resultImage;
ProgressBar progressBar;
Button btnImageCrop;

private static ImageCroppedCallBack callback;

public static Intent newIntent(Context context, String selectedFilePath, ImageCroppedCallBack imageCroppedCallBack) {
    Intent intent = new Intent(context, ImageCropActivity.class);
    intent.putExtra(FILE_DIR, selectedFilePath);
    //intent.putExtra("callback",callback);
    callback = imageCroppedCallBack;

    return intent;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_crop);
    documentScannerView = findViewById(R.id.document_scanner);
    btnImageCrop = findViewById(R.id.btnImageCrop);
    progressBar = findViewById(R.id.progressBar);
    resultImage = findViewById(R.id.result_image);

    String filePath = getIntent().getStringExtra(FILE_DIR);
    Bitmap bitmap = assetToBitmap(filePath);

    //ImageProcessingUtils.processImage(filePath, filePath);

    LoadListenerFunction onLoadListener = new LoadListenerFunction() {
        @Override
        public Unit invoke(Boolean loading) {
            // Your implementation here
            if (loading) {
                // Handle loading state
            } else {
                // Handle not loading state
            }
            progressBar.setVisibility(loading ? View.VISIBLE : View.GONE);
            return Unit.INSTANCE;
        }
    };

    documentScannerView.setOnLoadListener(onLoadListener);

    documentScannerView.setImage(bitmap);

    btnImageCrop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            progressBar.setVisibility(View.VISIBLE);
            // Simulate coroutine behavior using a separate thread or AsyncTask
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final Bitmap image = documentScannerView.getCroppedImage();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            progressBar.setVisibility(View.GONE);
                            resultImage.setVisibility(View.VISIBLE);
                            resultImage.setImageBitmap(image);
                            callback.onImageCropped(image);
                            finish();
                        }
                    });
                }
            }).start();
        }
    });
}

private Bitmap assetToBitmap(String file) {
    try {

        return BitmapFactory.decodeFile(file);
       

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

}
`

@markusGF
Copy link

markusGF commented Apr 2, 2024

I have the same problem.
Using java not kotlin. Implemented all like described but when use .setImage(bitmap) I got an NullPointerException.
The bitmap is given and valid, android studio show me the image in the debugger.
The following error was thrown:

FATAL EXCEPTION: main Process: de.app, PID: 29949 java.lang.NullPointerException at com.labters.documentscanner.DocumentScannerView.getLifecycle(DocumentScannerView.kt:46) at com.labters.documentscanner.DocumentScannerView.getScope(DocumentScannerView.kt:50) at com.labters.documentscanner.DocumentScannerView.doWhenInitialised(DocumentScannerView.kt:183) at com.labters.documentscanner.DocumentScannerView.setImage(DocumentScannerView.kt:76) at de.app.layout.CallbackFragments.ViewImageFragment.initViews(ViewImageFragment.java:153) at de.app.layout.CallbackFragments.ViewImageFragment.onCreateView(ViewImageFragment.java:97) at androidx.fragment.app.Fragment.performCreateView(Fragment.java:3114) at androidx.fragment.app.DialogFragment.performCreateView(DialogFragment.java:775) at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:557) at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:272) at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1943) at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1845) at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1782) at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:565) at android.os.Handler.handleCallback(Handler.java:942) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread.main(ActivityThread.java:8762) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants