I have a program that opens the camera via Intent to take a photo. That much part works fine already. However, I want it to save to a certain folder with a certain file name (the file name is optional but it would really really help).
So here's what I have so far.
Here's the line of code that opens the camera:
//TODO camera stuff.
Intent openCam = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//The two lines of code below were commented out at first.
//They were eventually added when I tried to save it with a custom name and destination
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
openCam.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
startActivityForResult(openCam, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
Result handler goes here:
//TODO handle result
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
System.out.println("I am here");
}
else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
}
else {
// Image capture failed, advise user
}
}
Before I implemented the two methods below, the code was working fine. However it was saved to the default folder with the default file name (the time stamp version). The toast displayed "Imaged saved to: null" since I haven't set that part yet.
So here are the methods that were supposed to handle the custom filename and destination
/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
Environment.getExternalStorageState();
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
*These code were lifted from the Camera Guide from developer.android.com.
The code above manages to open the Camera and take photos and save them. However, the problem occurs when the user decides to stop taking photos and press the back key. What happens is that the app force closes giving this error:
10-21 12:44:33.699: E/AndroidRuntime(13016): FATAL EXCEPTION: main
10-21 12:44:33.699: E/AndroidRuntime(13016): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.AIC.QRCodeScanner/com.AIC.QRCodeScanner.QRCodeScanner}: java.lang.NullPointerException
10-21 12:44:33.699: E/AndroidRuntime(13016): at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
10-21 12:44:33.699: E/AndroidRuntime(13016): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
10-21 12:44:33.699: E/AndroidRuntime(13016): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
10-21 12:44:33.699: E/AndroidRuntime(13016): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
10-21 12:44:33.699: E/AndroidRuntime(13016): at android.os.Handler.dispatchMessage(Handler.java:99)
10-21 12:44:33.699: E/AndroidRuntime(13016): at android.os.Looper.loop(Looper.java:123)
10-21 12:44:33.699: E/AndroidRuntime(13016): at android.app.ActivityThread.main(ActivityThread.java:3691)
10-21 12:44:33.699: E/AndroidRuntime(13016): at java.lang.reflect.Method.invokeNative(Native Method)
10-21 12:44:33.699: E/AndroidRuntime(13016): at java.lang.reflect.Method.invoke(Method.java:507)
10-21 12:44:33.699: E/AndroidRuntime(13016): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
10-21 12:44:33.699: E/AndroidRuntime(13016): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
10-21 12:44:33.699: E/AndroidRuntime(13016): at dalvik.system.NativeStart.main(Native Method)
10-21 12:44:33.699: E/AndroidRuntime(13016): Caused by: java.lang.NullPointerException
10-21 12:44:33.699: E/AndroidRuntime(13016): at com.AIC.QRCodeScanner.QRCodeScanner.onActivityResult(QRCodeScanner.java:379)
10-21 12:44:33.699: E/AndroidRuntime(13016): at android.app.Activity.dispatchActivityResult(Activity.java:3934)
10-21 12:44:33.699: E/AndroidRuntime(13016): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
10-21 12:44:33.699: E/AndroidRuntime(13016):
... 11 more
The line it points to would be this line (line 379): data.getData(), Toast.LENGTH_LONG
However, the files are saved at the folder /Pictures/MyCameraApp
together with the Instagram photos that were taken.
So the questions are:
1. Is there a way to make onActivityResult work properly? I know I can just resort to using startActivity in order not to kill the app.
2. Is there a way to only take one shot with the camera? So after the user saves the photo, the app goes back to the main activity.
3. Also, can I save it to a folder of my own? I'm not sure why it saves the photos in /Pictures/MyCameraApp
, I want it to save only to /MyCameraApp
.
I think I'm missing out on something simple here.
Here's the code that made it work:
//camera stuff
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
//folder stuff
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "QR_" + timeStamp + ".png");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(imageIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
It opens the camera and takes exactly one shot (it goes back to the main activity after the user saves the image that was taken. It saves the image to the specified folder.
You can get your photo in your folder but for that you will have to pass the filename with the intent using below code
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File out = Environment.getExternalStorageDirectory();
out = new File(out, imagename);
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
startActivityForResult(i, CAMERA_RESULT);
The out file contains the full path and the file name.
Now handle the onActivityResult() method and check the status of the result if the result is RESULT_OK then use the file name which you have supplied with the intent to access the photo.
You can refer How to get camera result as a uri in data folder? for more options.
Edit
You are getting data.getData()
to null because it will be null if you pass the file url with the intent. Indeed you have already file path which you are passing with the Intent so why you are trying to fetch it from the bundle in onActivityResult()
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With