-2

Activity results is NullPointerException, What I have done wrong or missed?

Photo gallery application(Eventually), followed the instructions on the android developer site about taking photos with inbuilt camera. onActivityResult giving null pointer. I am new to android so can't figure it out. Because of this it crashes, i expect it to show a bitmap in the activity.

public class aCamera extends AppCompatActivity {

    static  final  int REQUEST_IMAGE_CAPTURE = 1;
    static final int REQUEST_TAKE_PHOTO = 1;
    ImageView imageView;
    String currentPhotoPath;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a_camera);
        dispatchTakePictureIntent();

    }

    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) {

            File photoFile = null;
            try {
                photoFile = createImageFile();
                Log.i("DTPI-1", "dispatchTakePictureIntent: The IF AND TRY");
            } catch (IOException ex) {
                Log.i("DTPI-3" , "dispatchTakePictureIntent: catch");
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Log.i("photoURI", "dispatchTakePictureIntent: PHOTO FILE NOT EMPTY");
                Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.android.fileprovider",
                    photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                //setResult(RESULT_OK, takePictureIntent);
            }
        }
    }

    private File createImageFile() throws IOException {
        // Create an image file name
        Log.i("CIF -1", "createImageFile: Create FILE");
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",   /* suffix */
            storageDir      /* directory */
    );

        // Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = image.getAbsolutePath();
        return image;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i("Result", "onActivityResult: WE GET HERE");
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            imageView = findViewById(R.id.imageView);
            imageView.setImageBitmap(imageBitmap);
        }
}

Error logs.

2019-05-06 20:01:33.555 23363-23363/com.example.camera E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.camera, PID: 23363
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.camera/com.example.camera.aCamera}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4339)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4382)
    at android.app.ActivityThread.-wrap19(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1654)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:251)
    at android.app.ActivityThread.main(ActivityThread.java:6572)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
    at com.example.camera.aCamera.onActivityResult(aCamera.java:81)
    at android.app.Activity.dispatchActivityResult(Activity.java:7235)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4335)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4382) 
    at android.app.ActivityThread.-wrap19(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1654) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:251) 
    at android.app.ActivityThread.main(ActivityThread.java:6572) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Aalap Patel
  • 2,058
  • 2
  • 17
  • 31

3 Answers3

2

The camera saves the image to the photoUri that you supply in your Intent. In onActivityResult you have to get the Bitmap from that file.

Christian
  • 4,596
  • 1
  • 26
  • 33
  • Thanks, but this has made a different issue, it crashes before photo taken now rather than after. – deadstone1991 May 06 '19 at 18:52
  • how can a change in onActivityResult affect the app to crash before the photo is even taken? did you maybe change something else in addition? If the app crashes it is shown in LogCat, so you should post the Stacktrace here, but then it is a different question than the one answered here. – Christian May 06 '19 at 18:54
  • hope the log data above helps, this is before i made the aforementioned edit. @Christian – deadstone1991 May 06 '19 at 19:05
  • I think we misunderstood eachother. You need to do the change I mentioned as the Intent in onActivityResult will not always return data. This is the reason for your Null Pointer Exception. If the change causes a different exception (the app crashes), you need to show the Stacktrace of that to see the reason of the other crash. – Christian May 06 '19 at 19:30
  • I can try, but i am so new to this that i cant be sure i am doin it right. – deadstone1991 May 06 '19 at 19:47
  • i tried this. https://stackoverflow.com/questions/20782713/retrieve-bitmap-from-uri/20782887 but didnt help. – deadstone1991 May 06 '19 at 19:49
0

The line putExtra is the issue. Delete that line and receive bitmap in onActivityResult like mentioned below.

 @Override
protected void onActivityResult(int requestCode, int resultcode, Intent intent) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultcode == RESULT_OK) {
        Uri uri = intent.getData();
        Bitmap bitmap = null;
        try {
            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
        } catch (IOException e) {
            e.printStackTrace();
        }
        image.setImageBitmap(bitmap);
    }
}

Hope this works. You will see the explaination here on a post with similar issue.

Aalap Patel
  • 2,058
  • 2
  • 17
  • 31
0

As mentioned by @aalap patel, the issue is not with onActivityResult, it is with one line in dispatchTakePictureIntent method. takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); adding to this is the code that he also mentions.