0

Its very common question seen on StackOverflow and I have tried almost every solution on SO. I still get a NullPointerExeption in onAtivityResult- data.

I have tried:

The last code which I have tried for fourth time is given below:

On Button Click:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                File file=getOutputMediaFile(1);
                picUri = Uri.fromFile(file); // create
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, picUri); // set the image file
                    startActivityForResult(cameraIntent, REQUEST_TAKE_PHOTO);

getOutputMediaFile()

 private  File getOutputMediaFile(int type) {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "MyApplication");

        /**Create the storage directory if it does not exist*/
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }

        }
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == 1){
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_"+ timeStamp + ".png");
        } else {
            return null;
        }

        return mediaFile;
    }

onActivityResult()

 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == RESULT_OK && requestCode == REQUEST_TAKE_PHOTO && intent != null){


            Uri selectedImage = intent.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            //file path of captured image
            imagepath = cursor.getString(columnIndex);
            //file path of captured image
            File f = new File(imagepath);
            String filename = f.getName();
            cursor.close();
            Bitmap imageData = BitmapFactory.decodeFile(filename);
//            Bitmap imageData = (Bitmap) data.getExtras().get("data");

            Intent i = new Intent(this, EditImageActivity.class);
            i.putExtra("path", imageData );
            startActivity(i);
        }
    }
}

Please suggest something better which is not mentioned in above links which I already have referred because that didn't work for me.

I am using Android Atudio, with api 10+.

Crash log:

10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime: FATAL EXCEPTION: main
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime: Process: com.example.dell.drawdemo, PID: 25060
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.dell.drawdemo/com.example.dell.drawdemo.MainActivity}: java.lang.NullPointerException
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3593)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3636)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.ActivityThread.access$1300(ActivityThread.java:151)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:110)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:193)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5334)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:676)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:  Caused by: java.lang.NullPointerException
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at com.example.dell.drawdemo.MainActivity.onActivityResult(MainActivity.java:131)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.Activity.dispatchActivityResult(Activity.java:5546)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3589)
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3636) 
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.ActivityThread.access$1300(ActivityThread.java:151) 
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390) 
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:110) 
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:193) 
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5334) 
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:676) 
10-12 17:48:49.934 25060-25060/com.example.dell.drawdemo E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
Community
  • 1
  • 1
Drashti
  • 111
  • 2
  • 12
  • 10-12 17:20:27.534 17724-17724/com.example.dell.drawdemo E/AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.dell.drawdemo/com.example.dell.drawdemo.MainActivity}: java.lang.NullPointerException – Drashti Oct 12 '15 at 11:52
  • 10-12 17:20:27.534 17724-17724/com.example.dell.drawdemo E/AndroidRuntime: Caused by: java.lang.NullPointerException 10-12 17:20:27.534 17724-17724/com.example.dell.drawdemo E/AndroidRuntime: at com.example.dell.drawdemo.MainActivity.onActivityResult(MainActivity.java:129) – Drashti Oct 12 '15 at 11:55
  • hope this can help - @ Jas – Drashti Oct 12 '15 at 11:55
  • refer this link http://stackoverflow.com/questions/33078397/issue-in-capture-image-using-camera-in-lollipop/33078733#33078733 – Naveen Oct 12 '15 at 12:01
  • @DrashtiKapadia edit your question with logcat output , its confusing to read in comments – karan Oct 12 '15 at 12:02
  • logcat edited @Karan Mer – Drashti Oct 12 '15 at 12:20
  • Have already tried that... nullpointer showing. @Naveen – Drashti Oct 12 '15 at 12:21

2 Answers2

1

hi please try below code hope it will help you,

private final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
private final int MEDIA_TYPE_IMAGE = 1;

intent code for capture image

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
m_fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, m_fileUri);
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);


public Uri getOutputMediaFileUri(int p_type)
{
    return Uri.fromFile(getOutputMediaFile(p_type));
}


private File getOutputMediaFile(int p_type)
{
    File m_mediaFile;
    if (p_type == MEDIA_TYPE_IMAGE)
    {

            m_buffer = new StringBuffer();

            m_buffer.append(Environment.getExternalStorageDirectory().getPath() + File.separator).append("Image");
            File m_imageStorageFile = new File(m_buffer.toString());

            // Create the storage directory if it does not exist
            if (!m_imageStorageFile.exists())
            {
                if (!m_imageStorageFile.mkdirs())
                {
                    return null;
                }
            }
            // Create a media file name
            String m_timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new java.util.Date());
            m_buffer = new StringBuffer();
            m_buffer.append(m_imageStorageFile.getPath()).append(File.separator).append("IMG_").append(m_timeStamp).append(".jpg");
            m_mediaFile = new File(m_buffer.toString());

    }
    else
    {
        return null;
    }

    return m_mediaFile;
}

write below code in your onActivityResult

protected void onActivityResult(int p_requestCode, int p_resultCode, Intent p_data)
{
        super.onActivityResult(p_requestCode, p_resultCode, p_data);
        if (p_resultCode == RESULT_OK)
        {
            switch (p_requestCode)
            {
                case CAMERA_CAPTURE_IMAGE_REQUEST_CODE:
                    m_selectedPath = m_fileUri.getPath();
                break;

            }
        }
}

Constant.MEDIA_DIRECTORY_PATH contains your directory path example :- mnt/sdcard0/android/com.example

Mobile Team ADR-Flutter
  • 12,062
  • 2
  • 29
  • 49
0

You are getting NullPointerException because when user goes to camera intent and by the time he captures the image the activity on which camera intent was hosted gets destroyed or recreated when user comes back from the camera intent. Duw to which picUri (path of image created from camera intent) returns null as no such object is in memory right now.

Save path of image like this :

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {

    if(mImageCaptureUri!=null)
        savedInstanceState.putString("camera_image", picUri.toString());

    super.onSaveInstanceState(savedInstanceState);
}

And Retrieve image path from this :

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {

    if (savedInstanceState != null) {
        if (savedInstanceState.containsKey("camera_image")) {
            picUri = Uri.parse(savedInstanceState.getString("camera_image"));
        }
    }

    super.onRestoreInstanceState(savedInstanceState);
}

Referenced from already answered question.

Community
  • 1
  • 1
Vipul Asri
  • 8,395
  • 3
  • 46
  • 70
  • sorry m new in anddroid and i didnt use this two method can you please explain how to use and where to put this ? and if we use this then no need to use onActivityResult method? -@vipul_asri – Drashti Oct 12 '15 at 12:25
  • No, these methods are an addition to `onActivityResult()` method. You have to put these methods in the Activity just like `onActivityResult()` – Vipul Asri Oct 12 '15 at 12:53