-2

I am new to android development. This is a test app through which I am trying to invoke an image to my activity. Everything seems fine except when I am returning the camera Image in onACtivityResult(); the app crashes. I have included Logs so that I can point out the position the error is actually happening.

here is the Mainfest file:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-feature android:name="android.hardware.camera2"
    android:required="true"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.android.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"></meta-data>
    </provider>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>
        <intent-filter android:icon="@mipmap/ic_launcher_round">
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data
                android:scheme="http"
                android:host="www.example.com"
                android:pathPrefix="/nilabhra" />
        </intent-filter>
    </activity>
    <activity
        android:name=".DisplayMessageActivity"
        android:parentActivityName=".MainActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
        <intent-filter android:icon="@mipmap/ic_launcher_round">
            <action android:name="android.intent.action.SEND"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="text/plain"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.media.action.IMAGE_CAPTURE" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

This is the capture function:

public void capturePhoto(){
    Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (captureIntent.resolveActivity(getPackageManager())!= null){
            startActivityForResult(captureIntent, REQUEST_IMAGE_CAPTURE);
            Log.i(TAG, "capturePhoto: funtion ran succesfully");

    }

}

this is OnActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                if (requestCode == REQUEST_IMAGE_CAPTURE) {
                            if (resultCode == RESULT_OK) {
                Log.i(TAG, "onActivityResult: success");

            }

        } else {
            if (resultCode == RESULT_CANCELED) {
                Log.e(TAG, "onActivityResult: error and RESULT_CANCEllED TRUE");
            }
        }
}

and this is the Log after I take the shot form camera app and return the Intent:

03-07 12:44:49.963 27953-27953/myapp.test.nilabhra.com.freshstart E/AndroidRuntime: FATAL EXCEPTION: main
    Process: myapp.test.nilabhra.com.freshstart, PID: 27953
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {myapp.test.nilabhra.com.freshstart/myapp.test.nilabhra.com.freshstart.DisplayMessageActivity}: java.lang.NullPointerException: uri
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3717)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3760)
        at android.app.ActivityThread.access$1400(ActivityThread.java:153)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5451)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
        Caused by: java.lang.NullPointerException: uri
        at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:60)
        at android.content.ContentResolver.query(ContentResolver.java:476)
        at android.content.ContentResolver.query(ContentResolver.java:435)
        at myapp.test.nilabhra.com.freshstart.DisplayMessageActivity.onActivityResult(DisplayMessageActivity.java:164)
        at android.app.Activity.dispatchActivityResult(Activity.java:6528)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3713)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3760) 
        at android.app.ActivityThread.access$1400(ActivityThread.java:153) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5451) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Minas Mina
  • 2,058
  • 3
  • 21
  • 35
  • 6
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – ADM Mar 07 '18 at 07:24
  • The stack trace says you're doing a `ContentResolver` query in `onActivityResult()`, but your code shows no such thing. – Mike M. Mar 07 '18 at 07:26
  • You might need to set the target URI as an extra before you start the activity: captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); – Kackao Mar 07 '18 at 07:27
  • Which version of Android OS u r using ? – Sabyasachi Mar 07 '18 at 11:47
  • @MikeM. The ContentResolver I am using for a contact intent and yes , it is in onActivity results. Can't I put multiple results on a same onActivityresults? – Nilabhra Chakraborty Mar 07 '18 at 12:11
  • Yes, but that wasn't my point. You've not provided crucial info. The stack trace clearly shows that the `query()` method is throwing the `NullPointerException`. We need to see how you're calling it, what you're passing to it, etc. – Mike M. Mar 07 '18 at 12:15
  • @ss_ I am using marshmallow, and my project's minimum target is Jellybean – Nilabhra Chakraborty Mar 07 '18 at 12:16
  • I have omitted the contact intent an now app is not crushing after I return from the camera app but the Image is not showing in the ImageVIew. and the log.i() onActivityResult is not showing up in the logcat – Nilabhra Chakraborty Mar 07 '18 at 17:48

1 Answers1

1

Put this before startActivityForResult

Uri imageUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
Mohammad Arman
  • 508
  • 4
  • 13