I am using the following code to open an intent to capture and image and return the result with a specific request_code
Intent imageCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File mediaStorageDir=new File(Environment.getExternalStorageDirectory(),"zzz");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("zzz", "failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
Uri imageFile=Uri.fromFile(new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg"));
Log.d("zzz",""+imageFile); // to check whether filename is correct or not!!!
imageCapture.putExtra(MediaStore.EXTRA_OUTPUT, imageFile);
startActivityForResult(imageCapture, 1);
This intent shows the camera intent for capturing an image and does everything just as fine. It also captures the image, but just when the save button is clicked, it encounters a runtimeException.
03-17 09:34:32.324: E/AndroidRuntime(26771): FATAL EXCEPTION: main
03-17 09:34:32.324: E/AndroidRuntime(26771): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {idiot.houses.numberslider/idiot.houses.numberslider.MainActivity}: java.lang.NullPointerException
03-17 09:34:32.324: E/AndroidRuntime(26771): at android.app.ActivityThread.deliverResults(ActivityThread.java:3205)
03-17 09:34:32.324: E/AndroidRuntime(26771): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3248)
03-17 09:34:32.324: E/AndroidRuntime(26771): at android.app.ActivityThread.access$1200(ActivityThread.java:140)
03-17 09:34:32.324: E/AndroidRuntime(26771): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
03-17 09:34:32.324: E/AndroidRuntime(26771): at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 09:34:32.324: E/AndroidRuntime(26771): at android.os.Looper.loop(Looper.java:137)
03-17 09:34:32.324: E/AndroidRuntime(26771): at android.app.ActivityThread.main(ActivityThread.java:4921)
03-17 09:34:32.324: E/AndroidRuntime(26771): at java.lang.reflect.Method.invokeNative(Native Method)
03-17 09:34:32.324: E/AndroidRuntime(26771): at java.lang.reflect.Method.invoke(Method.java:511)
03-17 09:34:32.324: E/AndroidRuntime(26771): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
03-17 09:34:32.324: E/AndroidRuntime(26771): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
03-17 09:34:32.324: E/AndroidRuntime(26771): at dalvik.system.NativeStart.main(Native Method)
03-17 09:34:32.324: E/AndroidRuntime(26771): Caused by: java.lang.NullPointerException
03-17 09:34:32.324: E/AndroidRuntime(26771): at edu.vuum.mocca.ui.story.CreateStoryFragment.onActivityResult(CreateStoryFragment.java:316)
03-17 09:34:32.324: E/AndroidRuntime(26771): at edu.vuum.mocca.ui.story.StoryActivityBase.onActivityResult(StoryActivityBase.java:291)
03-17 09:34:32.324: E/AndroidRuntime(26771): at android.app.Activity.dispatchActivityResult(Activity.java:5372)
03-17 09:34:32.324: E/AndroidRuntime(26771): at android.app.ActivityThread.deliverResults(ActivityThread.java:3201)
03-17 09:34:32.324: E/AndroidRuntime(26771): ... 11 more
I think following code is helpful to you.
//code for calling intent
String fileName = "temp.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(intent, PICK_FROM_CAMERA);
//code write in onActivityResult() method
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK)
return;
switch (requestCode) {
case PICK_FROM_CAMERA:
String[] projection = { MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String capturedImageFilePath = cursor.getString(column_index_data);
break;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.