Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: startActivityForResult() from Fragment loses Data on Callback onActivityResult
    text
    copied!<p>Because showDialog() is decprecated, i have to use DialogFragments now to fix my Image Picker functionality. </p> <p>I got the Code from <a href="http://www.londatiga.net/it/how-to-create-android-image-picker/" rel="nofollow">here</a>, it worked like a charm so far.</p> <p>Now, to keep it simple I'll only show the necessery Codelines. First, after clicking a Button on <strong>Activity A</strong> to select an image with this a Dialog appears:</p> <pre><code>ImagePickerFragment ip= new ImagePickerFragment(); ip.show(getSupportFragmentManager(), "imagePicker"); </code></pre> <p>The Fragment Code looks like this:</p> <pre><code> public class ImagePickerFragment extends DialogFragment{ public Dialog onCreateDialog(Bundle savedInstanceState){ super.onCreate(savedInstanceState); //.. creating an adapter return new AlertDialog.Builder(getActivity()) .setTitle("select Image") .setAdapter(adapter, new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which){ if (which == 0){ // from camera Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File file = new File(Environment.getExternalStorageDirectory(), "picture_" + String.valueOf(System.currentTimeMillis()) + ".jpg"); mImageCaptureUri = Uri.fromFile(file); intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri); getActivity().startActivityForResult(intent, PICK_FROM_CAMERA); } else{ // pick from file Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); getActivity().startActivityForResult(Intent.createChooser(intent, "complete Action..."), PICK_FROM_FILE); } } }).create(): } } </code></pre> <p>Now, back in my <strong>Activity A</strong> the onActivityResult() Code looks like this:</p> <pre><code>protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != RESULT_OK) return; if (requestCode == PICK_FROM_FILE){ mImageCaptureUri = data.getData(); path = getRealPathFromURI(mImageCaptureUri); //from Gallery if (path == null) path = mImageCaptureUri.getPath(); //from File Manager } else path = data.getData().getPath(); **// this is were the Code starts when picture was taken with camera and where the exceptions come from..** if(path != null){ // do something with image } } </code></pre> <p>This actually works perfect for the Image picked from the galery, but for some reason not for the picture taken by the camera. After the Camera loads i can take a photo and when i accept that photo for saving, the app crashes.</p> <p>The requestCodes are correct, but i've found out that the Uri send back from the Intent's data is always null, even if i put it seperately in the intent (i.e. putExtra, bundle). I really wonder why it works for galery, though :-/</p> <p>Maybe one of you guys know what i did wrong?</p> <p>P.S: I'm sorry for my bad english.</p> <p><strong>Update</strong>: i was asked for the crash info.</p> <pre><code>09-06 01:58:50.929: E/AndroidRuntime(6322): FATAL EXCEPTION: main 09-06 01:58:50.929: E/AndroidRuntime(6322): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {android.pack.coding/android.myactivitys.MainActivity}: java.lang.NullPointerException 09-06 01:58:50.929: E/AndroidRuntime(6322): at android.app.ActivityThread.deliverResults(ActivityThread.java:2980) 09-06 01:58:50.929: E/AndroidRuntime(6322): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023) 09-06 01:58:50.929: E/AndroidRuntime(6322): at android.app.ActivityThread.access$1100(ActivityThread.java:123) 09-06 01:58:50.929: E/AndroidRuntime(6322): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177) 09-06 01:58:50.929: E/AndroidRuntime(6322): at android.os.Handler.dispatchMessage(Handler.java:99) 09-06 01:58:50.929: E/AndroidRuntime(6322): at android.os.Looper.loop(Looper.java:137) 09-06 01:58:50.929: E/AndroidRuntime(6322): at android.app.ActivityThread.main(ActivityThread.java:4424) 09-06 01:58:50.929: E/AndroidRuntime(6322): at java.lang.reflect.Method.invokeNative(Native Method) 09-06 01:58:50.929: E/AndroidRuntime(6322): at java.lang.reflect.Method.invoke(Method.java:511) 09-06 01:58:50.929: E/AndroidRuntime(6322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 09-06 01:58:50.929: E/AndroidRuntime(6322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 09-06 01:58:50.929: E/AndroidRuntime(6322): at dalvik.system.NativeStart.main(Native Method) 09-06 01:58:50.929: E/AndroidRuntime(6322): Caused by: java.lang.NullPointerException 09-06 01:58:50.929: E/AndroidRuntime(6322): at android.myactivitys.MainActivity.onActivityResult(MainActivity.java:298) 09-06 01:58:50.929: E/AndroidRuntime(6322): at android.app.Activity.dispatchActivityResult(Activity.java:4649) 09-06 01:58:50.929: E/AndroidRuntime(6322): at android.app.ActivityThread.deliverResults(ActivityThread.java:2976) 09-06 01:58:50.929: E/AndroidRuntime(6322): ... 11 more </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload