2

Because showDialog() is decprecated, i have to use DialogFragments now to fix my Image Picker functionality.

I got the Code from here, it worked like a charm so far.

Now, to keep it simple I'll only show the necessery Codelines. First, after clicking a Button on Activity A to select an image with this a Dialog appears:

ImagePickerFragment ip= new ImagePickerFragment(); 
ip.show(getSupportFragmentManager(), "imagePicker");

The Fragment Code looks like this:

 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():
     }
}

Now, back in my Activity A the onActivityResult() Code looks like this:

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
    }
}

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.

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 :-/

Maybe one of you guys know what i did wrong?

P.S: I'm sorry for my bad english.

Update: i was asked for the crash info.

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
user1650468
  • 21
  • 1
  • 3
  • Could you post the crash info? It sounds like its null pointer do to Intents data being null but just want to make sure. – Code Droid Sep 06 '12 at 01:36
  • btw deprecation does not mean you cannot use something. Its just a compiler warning. I still use showDialog() – Code Droid Sep 06 '12 at 01:38
  • I guess you are issuing two potential requests. Are you getting back a response to each of these? – Code Droid Sep 06 '12 at 01:40
  • Hi Code Droid, thx for your reply. I know its just a warning, but my professor mentioned that he dont want to see any deprecated code. I will post the crash info asap. what do you mean by two potential requests? thank you very much! – user1650468 Sep 06 '12 at 01:53
  • There is an issue in the camera for certain devices, http://stackoverflow.com/questions/15248265/camera-intent-not-working-with-samsung-galaxy-s3/15287164#15287164 – Skynet Dec 24 '13 at 04:49

0 Answers0