1

In my android app, user can pick an image from gallery. The image is then shown in a dialog and further actions can be taken on image. But i am getting NPE for some images. Please NOTE i get NPE only for some images (not sure what those images have in special)

I have seen people getting similar error but the solutions mentioned are not working for me. In my case, the image that user is picking is on SD card and i get the following error:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=9002, result=-1, data=Intent { dat=file:///storage/emulated/0/DCIM/Camera/IMG_20150826_121419.jpg typ=image/jpeg }} to activity {com.myapp.androidapp/com.myapp.androidapp.TTMainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference
       at android.app.ActivityThread.deliverResults(ActivityThread.java:3540)
       at android.app.ActivityThread.handleSendResult(ActivityThread.java:3583)
       at android.app.ActivityThread.access$1300(ActivityThread.java:147)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5232)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

    Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference
           at com.myapp.camera.TTCamera.onActivityResult(TTCamera.java:60)
           at com.myapp.androidapp.TTMainActivity.onActivityResult(TTMainActivity.java:88)
           at android.app.Activity.dispatchActivityResult(Activity.java:6186)
           at android.app.ActivityThread.deliverResults(ActivityThread.java:3536)
           at android.app.ActivityThread.handleSendResult(ActivityThread.java:3583)
           at android.app.ActivityThread.access$1300(ActivityThread.java:147)
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
           at android.os.Handler.dispatchMessage(Handler.java:102)
           at android.os.Looper.loop(Looper.java:135)
           at android.app.ActivityThread.main(ActivityThread.java:5232)
           at java.lang.reflect.Method.invoke(Method.java)
           at java.lang.reflect.Method.invoke(Method.java:372)
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

The NPE comes at the line which has the code:

cursor.moveToFirst();

Basically the cursor is coming out to be null.

Here is the code that i use to decode the result:

 if (requestCode == TTConfig.LOAD_IMAGE_FROM_GALLERY_ACTIVITY_REQUEST_CODE && resultCode == activity.RESULT_OK && null != data){

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

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

             int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
             String picturePath = cursor.getString(columnIndex);
             cursor.close();
}

Here is the code which i use to fire intent:

intent = new Intent(Intent.ACTION_PICK,
                 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
         activity.startActivityForResult(intent, TTConfig.LOAD_IMAGE_FROM_GALLERY_ACTIVITY_REQUEST_CODE);
Rony
  • 39
  • 3
  • check with this http://stackoverflow.com/questions/29144367/file-path-return-null-always-in-lollipop-android – Pavan Sep 03 '15 at 16:51
  • @pavan As i said, users are getting the NPE only on some images. Most of the users on lollipop are able to pick images nicely. I feel the issue is something different. – Rony Sep 03 '15 at 17:19
  • Maybe. But you should of course add code to check if cursor is null. And handle accordingly. You realise that for most actions on the image you can use the url? You don't need String picturePath. – greenapps Sep 03 '15 at 18:50
  • @greenapps: Null check can be used but that deteriorates the user experience. Ideally i would like to avoid the situation.As the image is present in device, there are hardly any reasons for this behaviour. Yes i do realize i can use URL for most actions but i am worried about the places where url does not work. Can you help me understand the areas where URL does not work? – Rony Sep 04 '15 at 08:12
  • You are neglecting my words on using uri directly. Any reason? – greenapps Sep 04 '15 at 08:14
  • @greenapps : in an ideal world :) i would like to understand the reason behind this erratic behaviour and take appropriate steps to avoid it. – Rony Sep 04 '15 at 08:47
  • So i understand that you don't want to take 'appropriate steps'.now. Well that's up to you. But you could have commented on using the uri though. – greenapps Sep 04 '15 at 10:45

0 Answers0