Save Photo from Camera in Android, Fix NullPointerException

25 71



Save a picture to the SD Card from the camera in Android by using ANDROID_IMAGE_CAPTURE and Intent.putExtra. This also fixes a potential NullPointerException when you use the camera and (Bitmap) data.getExtras().get(“data”);

Important Note: If you are using Android 24 or greater, you must also consider FileProvider and a new Permissions model. I have a video that describes that here:

Source code:
This video is part of a playlist, available here:

25 Comments
  1. Jim Thunder says

    When I click my camera button, the app goes back a page and I don't know why. Normally it would just bring the camera up but it basically crashes. Any idea why?

  2. Shivam Bulsara says

    Definitely Useful Information and Program Works Great ! Thanks ! Subscriber + 1 !!!

  3. Raquel Aranda says

    Gracias de nuevo. Todo perfecto, aunque me daba un error que solucioné poniendo esto en el btnPakePhotoClicked:
    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());
    La ruta de las fotos yo la tengo en: Device File Explorer/storage/self/primary/Pictures

  4. Brandan Jones says

    Important Note: If you are using Android 24 or greater, you must also consider FileProvider and a new Permissions model. I have a video that describes that here: https://www.youtube.com/watch?v=_xIWkCJZCu0&feature=youtu.be

  5. Tatang Sunaryo says

    Thank's, Work great

  6. David Gleave says

    Brilliant Guide! Thank you very much!

  7. Soham Choudhury says

    Throws fileuriexposed exception for API 24 and higher

  8. Justin Chitla says

    So how would you save to SD card as well as set the image view?

  9. jeremytodd1 says

    I don't believe this method works on more up to date phones. I have a phone running Oreo currently and I get an error: FATAL EXCEPTION: main
    Process: com.example.jeremy.cameratest, PID: 4037
    android.os.FileUriExposedException: file:///storage/emulated/0/Pictures/CameraTest20171002_120420.jpg exposed beyond app through ClipData.Item.getUri()

    Any idea how to fix it? It works fine on older Android versions but Oreo not so much.

  10. naserharbi says

    Hi. I tried your example and ran it on my android phone. I don't see the saved file! everything runs but no file at the end! Can you shed more light here?This is what I wrote—>string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + "_IMG.jpg";
    Intent takePictureIntent = new Intent(MediaStore.ActionImageCapture);
    File picturesDirectory = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures);
     File fi = new File(picturesDirectory, filename);
     Java.Net.URI picUri = fi.ToURI();
     takePictureIntent.PutExtra(MediaStore.ExtraOutput, picUri); StartActivityForResult(takePictureIntent, 10);

  11. Jitendra Kumar Jena says

    Thank you man.Genius

  12. Anditya Harman says

    thanks
    .
    its works

  13. Ramzan Tumgojev says

    Thank you! Was getting frustrated about this exception

  14. Rutanshu Jhaveri says

    Why did you add the save button as an option if the image is directly stored when it is clicked. I want the image stored only when I click the option of save

  15. Hussain Albader says

    Hi…. i did the same thing but i want the picture to be saved on my real device not on the emulator i get this error
    FATAL EXCEPTION: main
    Process: com.halbader.treasurehunt, PID: 13687
    java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
    Can some one help me

  16. ibrahim alazzawi says

    hey can you help me my app is cashing every time a press the button that take me to the camera,im getting security exception which says" revoked permission android.permission.CAMERA"

  17. ibrahim alazzawi says

    this was really useful thanks i subscribed keep it up

  18. simran sachdeva says

    hi brandon…..you are working really nice….I need your favour plz help me as soon as possible. I want to save the captured image and also i want to show that image in image view…….

  19. Konrad Zając says

    Hi Brandan, how to display the captured image and program the button for saving the photo? I do not want to save it automatically as you did. Can you help me? Thank in advance, Konrad.

  20. Bazza Cuda says

    Hi Brandon. Great videos, thanks ! The Github repository PlantPlaces15s305 doesn't appear to contain the yellowgreenbevel9.png image files. Is that deliberate ?

  21. Anwesh dangol says

    How to get the saved image without opening the gallery as you did in other videos. I need to get the image captured and show in ImageView as you said at the end of this video. Any help would really be appreciated !

  22. Kiva John says

    hey Brandan, u missed the github link and how to get thumbnail ?

  23. kishlay raj says

    thanks alot bro

  24. Edvard åkerberg says

    Works Great!

  25. Malshan Gunawardana says

    How to fix that error ?

Leave A Reply

Your email address will not be published.