0

Working with cameras, I have a option that lets users take photos as well as browse photos from their gallery. The image is then loaded in the app screen. The problem I am having currently is with the take photos option.

The take photos option works well on devices with KitKat but other than that it is a disaster.

The code is the following:

takePhoto.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // file to save the image
        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

        // set the image file name
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

        startActivityForResult(intent, TAKE_PICTURE);

    }

Where the getOutputMediaFileUri function is similar to the code here.

and on the onActivityResult() method:

if (requestCode == TAKE_PICTURE) {
    if (resultCode == activity.RESULT_OK) {             
        String path = fileUri.getPath().toString(); 
        previewImage(path);
        try {
            getPictureDetails(path);
        } catch (FileNotFoundException e) {
            //TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

Error log:

11-29 14:42:12.750: E/AndroidRuntime(18570): FATAL EXCEPTION: main
11-29 14:42:12.750: E/AndroidRuntime(18570): java.lang.RuntimeException: Unable to resume activity {com.developmentcheck.dcforpublic/com.developmentcheck.dcforpublic.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=262344, result=-1, data=null} to activity {com.developmentcheck.dcforpublic/com.developmentcheck.dcforpublic.MainActivity}: java.lang.NullPointerException
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2658)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2686)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2159)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3595)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.ActivityThread.access$800(ActivityThread.java:146)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1266)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.os.Looper.loop(Looper.java:137)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.ActivityThread.main(ActivityThread.java:4949)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at java.lang.reflect.Method.invokeNative(Native Method)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at java.lang.reflect.Method.invoke(Method.java:511)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at dalvik.system.NativeStart.main(Native Method)
11-29 14:42:12.750: E/AndroidRuntime(18570): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=262344, result=-1, data=null} to activity {com.developmentcheck.dcforpublic/com.developmentcheck.dcforpublic.MainActivity}: java.lang.NullPointerException
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3224)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2645)
11-29 14:42:12.750: E/AndroidRuntime(18570):    ... 13 more
11-29 14:42:12.750: E/AndroidRuntime(18570): Caused by: java.lang.NullPointerException
11-29 14:42:12.750: E/AndroidRuntime(18570):    at com.developmentcheck.dcforpublic.Fragment_comment.onActivityResult(Fragment_comment.java:358)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:166)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.Activity.dispatchActivityResult(Activity.java:5369)
11-29 14:42:12.750: E/AndroidRuntime(18570):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3220)
11-29 14:42:12.750: E/AndroidRuntime(18570):    ... 14 more

The error occurs in the following line:

String path = fileUri.getPath().toString(); 
Rakeeb Rajbhandari
  • 5,043
  • 6
  • 43
  • 74
  • 1
    Your error is on `11-29 14:42:12.750: E/AndroidRuntime(18570): at com.developmentcheck.dcforpublic.Fragment_comment.onActivityResult(Fragment_comment.java:358)`. Check Fragment_comment on 358 – vinothp Nov 29 '13 at 09:15
  • @Vino yes I know that ... I pointed out the error. – Rakeeb Rajbhandari Nov 29 '13 at 09:17
  • Where is `fileUri` declared? Your activity is restarted and you lose its value, I guess... – Szymon Nov 29 '13 at 09:26
  • I assume in some devices this happens due to orientation problem.Where the activity get restarted.What you can do is save the file uri in `Shared Preferences` until you captured the image.. – vinothp Nov 29 '13 at 09:27
  • @Szymon its declared as a global variable. If it were to loose the value, it wouldn't have worked on other devices ?? Because it did work on devices with Kitkat – Rakeeb Rajbhandari Nov 29 '13 at 09:33
  • 1
    When switching from one `Activity` to another one, the OS is free to destroy the previous one for various reasons, such as lack of memory etc. When you start another activity for result, you are in this situation. When you come back, if the activity was destroyed, you need to make sure that it gets re-constructed properly. So yes, it is relevant that you show how `fileUri` is defined and how it's restored when you switch back to this `Activity`. – frozenkoi Nov 29 '13 at 09:51

2 Answers2

0

In your onActivityResult first get fileUri from data intent returned

     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE) {
    if (resultCode == activity.RESULT_OK) {             
 fileUri =data.getExtras(MediaStore.EXTRA_OUTPUT);       
String path = fileUri.getPath().toString(); 
        previewImage(path);
        try {
            getPictureDetails(path);
        } catch (FileNotFoundException e) {
            //TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

     }
vipul mittal
  • 17,343
  • 3
  • 41
  • 44
0

Change your fileUri from

 fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

to

            String fileName = "temp.jpg";  
            ContentValues values = new ContentValues();  
            values.put(MediaStore.Images.Media.TITLE, fileName);  
            fileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Abhishek Agarwal
  • 1,907
  • 12
  • 21