-2

I'm trying to get my app to start a camera intent in order to take a picture and save it into a directory as well as show a thumbnail in the main view, but I don't quite seem to get it right. Here's the methods I'm using:

@RequiresApi(api = Build.VERSION_CODES.M)
private void dispatchTakePictureIntent() {
    if (ContextCompat.checkSelfPermission(getContext(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{android.Manifest.permission.CAMERA},
                    5);
        }
    }

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getContext().getPackageManager()) != null) {
        File photoFile = null;

        try {
            photoFile.createNewFile();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        if(photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(getContext(),
                    "com.example.android.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

And here's the OnActivityResult:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        image.setImageBitmap(imageBitmap);
    }

}

I seem to be getting a NullPointerException in two different places, at:

  • photoFile.createNewFile();
  • image.setImageBitmap(imageBitmap);

How can I fix this?

Lal Krishna
  • 15,485
  • 6
  • 64
  • 84
Adrián Valero
  • 29
  • 1
  • 2
  • 9
  • Possible duplicate of [Android Camera : data intent returns null](https://stackoverflow.com/questions/9890757/android-camera-data-intent-returns-null) – Nigam Patro Nov 21 '17 at 12:27

3 Answers3

0

NullPointerException in two different places, at photoFile.createNewFile();

use this

 File photoFile = new File(Environment.getExternalStorageDirectory() + File.separator + "filename");

instead of this

File photoFile = null;

NullPointerException in two different places, at image.setImageBitmap(imageBitmap);

just Intilize your ImageView

Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
0

File photoFile = null — since you are assigning null to photoFile, you will get a NullPointerException whenever you try calling methods on photoFile. You need to set photoFile to some File that is covered by your FileProvider configuration, since you are using FileProvider. Also note that you can get rid of the createNewFile() call, as that is not necessary.

And, since you are using EXTRA_OUTPUT, extras.get("data") will return null. Your image is supposed to be in the location indicated by photoFile.

Here is a sample app demonstrating the use of ACTION_IMAGE_CAPTURE with FileProvider.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Change your onActivityResult to

  @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == Activity.RESULT_OK) {  
                    onCaptureImageResult(data);
            }
        }

for setting in a thumbnail pass the data in your ImageView variable like this:

private void onCaptureImageResult(Intent data) {

        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
        byteArray = bytes.toByteArray();
        encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
        File destination = new File(Environment.getExternalStorageDirectory(),
                System.currentTimeMillis() + ".jpg");
        FileOutputStream fo;
        try {
            destination.createNewFile();
            fo = new FileOutputStream(destination);
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        imageView.setImageBitmap(thumbnail);
    }
Vikas singh
  • 3,461
  • 3
  • 14
  • 28