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);