简体   繁体   中英

How to convert an image to bitmap

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)

Try:

InputStream is = getContentResolver().openInputStream(result);
        
Bitmap bitmap = BitmapFactory.decodeStream(is);

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);
                String path = result.getPath();
              bitmap = BitmapFactory.decodeFile(path);
             if (bitmap != null) {
                    int pixel = bitmap.getPixel(0, 2);
                   String cPixel = String.valueOf(pixel);
                   textView.setText(cPixel); // 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM