0

I want to select a picture/image from gallery and then perform some operation on the pixels of the selected image. The image would be displayed on an image view. I learned that in order to operate on the pixels of an image, the image should be converted to bitmap. I can select an image, but my I keep getting an exception at runtime when the image is about to be converted to bitmap. Here's my code:

resultLauncher = registerForActivityResult(new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>() {
            @Override
            public void onActivityResult(Uri result) {
                imageView.setImageURI(result);
                String path = result.getPath();
                bitmap = BitmapFactory.decodeFile(path);
                int pixel = bitmap.getPixel(0, 2);
                String cPixel = String.valueOf(pixel);
                textView.setText(cPixel); // just to check if it works 

And my exception stack trace:

java.lang.RuntimeException: Unable to resume activity {com.example.bitmapprac/com.example.bitmapprac.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getPixel(int, int)' on a null object reference
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3718)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3761)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1702)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6739)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:449)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getPixel(int, int)' on a null object reference
        at com.example.bitmapprac.MainActivity$1.onActivityResult(MainActivity.java:50)
        at com.example.bitmapprac.MainActivity$1.onActivityResult(MainActivity.java:44)
        at androidx.activity.result.ActivityResultRegistry$1.onStateChanged(ActivityResultRegistry.java:148)
        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
        at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:265)
        at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:307)
        at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:148)
        at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:134)
        at androidx.lifecycle.ReportFragment.dispatch(ReportFragment.java:68)
        at androidx.lifecycle.ReportFragment.dispatch(ReportFragment.java:144)
        at androidx.lifecycle.ReportFragment.onStart(ReportFragment.java:109)
        at android.app.Fragment.performStart(Fragment.java:2534)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1315)
        at android.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1557)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1622)
        at android.app.FragmentManagerImpl.dispatchMoveToState(FragmentManager.java:3043)
        at android.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:3000)
        at android.app.FragmentController.dispatchStart(FragmentController.java:189)
        at android.app.Activity.performStart(Activity.java:7071)
        at android.app.Activity.performRestart(Activity.java:7140)
        at android.app.Activity.performResume(Activity.java:7145)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3693)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3761) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1702) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6739) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:449) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

 

jefmat95
  • 28
  • 5
  • Please post your exception stack trace – Mahdi Yusefi Dec 10 '21 at 19:22
  • Alright, Mahdi Yusefi, I have. – jefmat95 Dec 10 '21 at 20:35
  • 1
    `Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getPixel(int, int)' on a null object reference ` – blackapps Dec 10 '21 at 20:37
  • @KaruneshPalekar, the variable `result` is of type `Uri` and class `Uri` doesn't have any method as `absolutePath()`. – jefmat95 Dec 10 '21 at 20:54
  • @blackapps please how do I fix this exception? Point me in the right direction please – jefmat95 Dec 10 '21 at 20:56
  • MY bad ,I though you had a file from which you generating thr URI , what @blackapps has mentioned is the core cause of your error . The error says that the bitmap is empty , so first make sure that your bitmap is not null . – Karunesh Palekar Dec 10 '21 at 21:09
  • 1
    `how do I fix this exception?` By not calling bitmap.getPixel when bitmap==null. Check for null before use. After that your question would be: why is BitmapFactory.decodeFile() returning null? – blackapps Dec 10 '21 at 21:47
  • @jefmat95 A possibility could be that your selected image is too large. I refer you to check this [link](https://stackoverflow.com/questions/3879992/how-to-get-bitmap-from-an-uri) – Mahdi Yusefi Dec 11 '21 at 07:21
  • @blackapps Yeah, I have checked for null and have seen that `BitmapFactory.decodefile()` is returning null. Perhaps `result.getPath()` cannot be decoded. – jefmat95 Dec 11 '21 at 08:16
  • uri.getPath() is useless. Have a look at its value and tell us. Its no path. You cannot use the File class for it. – blackapps Dec 11 '21 at 09:38
  • `I keep getting an exception at runtime when the image is about to be converted to bitmap.` No. You know now that null is returned. The exception is from using a null. Please update your post. – blackapps Dec 11 '21 at 09:40
  • `textView.setText(cPixel); // just to check if it works ` Take an ImageView and use `imageView.setImageUri(result)` or `imageView.setImageBitmap(bitmap);` (the bitmap from my answer). – blackapps Dec 11 '21 at 10:06

2 Answers2

1

Try:

InputStream is = getContentResolver().openInputStream(result);
        
Bitmap bitmap = BitmapFactory.decodeStream(is);
blackapps
  • 8,011
  • 2
  • 11
  • 25
0

I edited your code, so your final code should look like this:

        resultLauncher = registerForActivityResult(new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>()){
    @Override
    public void onActivityResult(Uri result) {
        imageView.setImageURI(result);
        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(result));
        if (bitmap != null) {
            textView.setText(String.valueOf(bitmap.getPixel(0, 2))); // just to check if it works
        }
    }
    }

Please try it and comment if it worked or not, and accept this answer if it worked so more people can find it.

Edit 1: I edited the code, please see it and try it.

Omar Hemaia
  • 188
  • 10
  • The `if` statement there handled the exception. But my `textview` doesn't display any pixel which means that `BitmapFactory.decodefile()` is returning null. I need to find out why it is returning null and how to fix it. – jefmat95 Dec 11 '21 at 08:11
  • `But my textview doesn't display any pixel ` TextViews are for text. They cannot handle bitmaps. – blackapps Dec 11 '21 at 10:04
  • @jefmat95 I edited my answer, please see the code and try it. – Omar Hemaia Dec 11 '21 at 21:29