Taking a picture with a camera intent and saving it to a file

Adrián Valero

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?

vikas singh

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);
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Camera is not saving after taking picture

From Dev

Android- taking a picture for temporary use with the camera intent

From Dev

Taking picture with Android Camera Preview and saving in SQL database is crashing

From Dev

android: camera is not taking a picture

From Dev

Picture file captured with camera intent empty for a while in onActivityResult

From Dev

Stock Camera Intent resulting as RESULT_OK but not actually saving the file

From Dev

Taking a photo and saving it to the Camera Roll

From Dev

Sent camera picture via intent

From Dev

Taking a picture in my app and saving it in the gallery

From Dev

Camera crashes after taking picture and hitting ok

From Dev

Capture text from camera without taking picture

From Dev

How to know Camera Is completed taking taking picture In ios

From Dev

Taking picture without SurfaceView or without Photo Intent Activity

From Dev

Taking Picture via intent to internal (app) storage returns null for bitmap

From Dev

Taking Picture via intent to internal (app) storage returns null for bitmap

From Dev

How to force user to use the "square" camera filter when taking a picture

From Dev

Change the text for buttons when taking picture via Camera using UIIMagePickerController

From Dev

SurfaceView freezes for 2 seconds when taking picture with back camera

From Dev

Image not setting in ImageView after taking picture from Camera

From Dev

Taking picture without camera preview not working in lollipop and above version

From Dev

Android: taking a picture without calling local Camera app

From Dev

Activity rotates after taking camera through intent in S4

From Dev

Android: take camera picture intent remove confirmation dialog

From Dev

how to start camera intent and save a non-compressed picture

From Dev

Take picture only with android camera intent in Kotlin, not video

From Dev

Android Tablet Take Picture Camera Intent Activity Restarts

From Dev

I want to open the android camera without saving the picture to gallery

From Dev

Taking pictures using camera and applying image filters to the picture But filters are taking time to apply on to the image

From Dev

OpenCV isn't saving 2nd webcam picture after taking 1st webcam picture

Related Related

  1. 1

    Camera is not saving after taking picture

  2. 2

    Android- taking a picture for temporary use with the camera intent

  3. 3

    Taking picture with Android Camera Preview and saving in SQL database is crashing

  4. 4

    android: camera is not taking a picture

  5. 5

    Picture file captured with camera intent empty for a while in onActivityResult

  6. 6

    Stock Camera Intent resulting as RESULT_OK but not actually saving the file

  7. 7

    Taking a photo and saving it to the Camera Roll

  8. 8

    Sent camera picture via intent

  9. 9

    Taking a picture in my app and saving it in the gallery

  10. 10

    Camera crashes after taking picture and hitting ok

  11. 11

    Capture text from camera without taking picture

  12. 12

    How to know Camera Is completed taking taking picture In ios

  13. 13

    Taking picture without SurfaceView or without Photo Intent Activity

  14. 14

    Taking Picture via intent to internal (app) storage returns null for bitmap

  15. 15

    Taking Picture via intent to internal (app) storage returns null for bitmap

  16. 16

    How to force user to use the "square" camera filter when taking a picture

  17. 17

    Change the text for buttons when taking picture via Camera using UIIMagePickerController

  18. 18

    SurfaceView freezes for 2 seconds when taking picture with back camera

  19. 19

    Image not setting in ImageView after taking picture from Camera

  20. 20

    Taking picture without camera preview not working in lollipop and above version

  21. 21

    Android: taking a picture without calling local Camera app

  22. 22

    Activity rotates after taking camera through intent in S4

  23. 23

    Android: take camera picture intent remove confirmation dialog

  24. 24

    how to start camera intent and save a non-compressed picture

  25. 25

    Take picture only with android camera intent in Kotlin, not video

  26. 26

    Android Tablet Take Picture Camera Intent Activity Restarts

  27. 27

    I want to open the android camera without saving the picture to gallery

  28. 28

    Taking pictures using camera and applying image filters to the picture But filters are taking time to apply on to the image

  29. 29

    OpenCV isn't saving 2nd webcam picture after taking 1st webcam picture

HotTag

Archive