在我的Android应用中,用户可以从图库中选择图像。然后在对话框中显示图像,并且可以对图像采取进一步的动作。但我正在为一些图像获取NPE。请注意我只为某些图像获取NPE(不确定这些图像的特殊情况)
我看到有人遇到类似的错误,但提到的解决方案对我不起作用。在我的情况下,用户正在挑选的图像在SD卡上,我收到以下错误:
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)
NPE来自包含以下代码的行:
cursor.moveToFirst();
基本上光标是空的。
以下是我用来解码结果的代码:
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();
}
以下是我用来触发意图的代码:
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);