Save Photo from Camera in Android, Fix NullPointerException
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:
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?
Definitely Useful Information and Program Works Great ! Thanks ! Subscriber + 1 !!!
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
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
Thank's, Work great
Brilliant Guide! Thank you very much!
Throws fileuriexposed exception for API 24 and higher
So how would you save to SD card as well as set the image view?
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.
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);
Thank you man.Genius
thanks
.
its works
Thank you! Was getting frustrated about this exception
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
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
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"
this was really useful thanks i subscribed keep it up
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…….
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.
Hi Brandon. Great videos, thanks ! The Github repository PlantPlaces15s305 doesn't appear to contain the yellowgreenbevel9.png image files. Is that deliberate ?
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 !
hey Brandan, u missed the github link and how to get thumbnail ?
thanks alot bro
Works Great!
How to fix that error ?