从图库中选择图片后,Android应用程序崩溃

时间:2013-03-31 22:51:27

标签: android image select gallery

我使用以下代码让用户从他的图库中选择一张图片。但是在选择了img后,应用程序崩溃了。

Intent i = new Intent(
        Intent.ACTION_PICK,
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(i, RESULT_LOAD_IMAGE);

继承崩溃的logcat:

04-01 00:58:53.210: E/AndroidRuntime(26970): FATAL EXCEPTION: main
04-01 00:58:53.210: E/AndroidRuntime(26970): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/21931 flg=0x1 }} to activity {de.arvidg.exampleactionbartabs/de.arvidg.exampleactionbartabs.AddNewMission}: java.lang.NullPointerException
04-01 00:58:53.210: E/AndroidRuntime(26970):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3310)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3353)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at android.app.ActivityThread.access$1100(ActivityThread.java:145)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1259)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at android.os.Looper.loop(Looper.java:137)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at android.app.ActivityThread.main(ActivityThread.java:4978)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at java.lang.reflect.Method.invokeNative(Native Method)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at java.lang.reflect.Method.invoke(Method.java:511)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at dalvik.system.NativeStart.main(Native Method)
04-01 00:58:53.210: E/AndroidRuntime(26970): Caused by: java.lang.NullPointerException
04-01 00:58:53.210: E/AndroidRuntime(26970):    at de.arvidg.exampleactionbartabs.AddNewMission.onActivityResult(AddNewMission.java:131)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at android.app.Activity.dispatchActivityResult(Activity.java:5192)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3306)
04-01 00:58:53.210: E/AndroidRuntime(26970):    ... 11 more

继承了发生异常的onActivityResult。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
            && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

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

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

        cursor.close();

        ImageView imageView = (ImageView) findViewById(R.id.chosenpictureview);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        filepath = picturePath;
        thread.setPathToOurFile(picturePath);


    }

}

1 个答案:

答案 0 :(得分:0)

onActivityResult()中,您还可以直接从Uri selectedImage打开所选图片(使用getData()),而不是使用MediaStore查询。


相机或图库

如果您希望允许用户从图库或相机中进行选择,请务必查看此信息:Allow user to select camera or gallery for image