-1

I am trying to send an image back to the Main Activity from another activity after taking then image from the gallery in that activity.The app crashes whilst doing this. How can i solve this? This is my code

Second Activity

Intent returnIntent = new Intent();

                        String str = filePath.toString();
                        returnIntent.putExtra("UPDATED_PIC", str);
                        setResult(RESULT_OK,returnIntent);

Main Activity

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == PROFILE_PICTURE_REQUEST) {
                if (resultCode == RESULT_OK) {
                    if(data != null) {


                        String str = getIntent().getStringExtra("UPDATED_PIC");
                         //Getting Error here                       
                         filePath = Uri.parse(str);

                        try {
                            //Getting the Bitmap from Gallery
                            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                            // Setting the Bitmap to ImageView
                            mNetworkImageView.setImageBitmap(bitmap);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }

        }

Error Log

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.admin.profilepicture, PID: 4975
                  java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.admin.profilepicture/com.example.admin.profilepicture.MainActivity}: java.lang.NullPointerException: uriString
                      at android.app.ActivityThread.deliverResults(ActivityThread.java:4089)
                      at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)
                      at android.app.ActivityThread.-wrap20(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                   Caused by: java.lang.NullPointerException: uriString
                      at android.net.Uri$StringUri.<init>(Uri.java:475)
                      at android.net.Uri$StringUri.<init>(Uri.java)
                      at android.net.Uri.parse(Uri.java:437)
                      at com.example.admin.profilepicture.MainActivity.onActivityResult(MainActivity.java:61)
                      at android.app.Activity.dispatchActivityResult(Activity.java:6932)
                      at android.app.ActivityThread.deliverResults(ActivityThread.java:4085)
                      at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132) 
                      at android.app.ActivityThread.-wrap20(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Pfil Mintz
  • 161
  • 6
  • 16

2 Answers2

2

you need to write this code on onActivityResult() of MainActivity

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    String str = data.getStringExtra("UPDATED_PIC");
    filePath = Uri.parse(str);
}

use data in place of getIntent() in your onActivityResult()

and while calling the SecondActivity you should pass request code also

take a look at this reference question

Community
  • 1
  • 1
Ravi
  • 34,851
  • 21
  • 122
  • 183
1

Instead of using getIntent() try using data.getStringExtra() in your onActivityResult()

Srijith
  • 1,695
  • 17
  • 29