从SD卡获取图像并将其发送到下一个活动

时间:2014-05-31 22:39:32

标签: android

@Override
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.imageView10);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
}

我使用此代码从SD卡加载图像。问题是,当我点击添加按钮时,它会显示我手机的图库但是当我选择图像时它显示我强制关闭错误...
我在ListView中显示此图像,如何调整图像大小?也许我的形象太大了,这就是为什么它不适合我的ListView。

LogCat:

06-01 04:51:09.793: W/KeyCharacterMap(5401): No keyboard for id 0
06-01 04:51:09.793: W/KeyCharacterMap(5401): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-01 04:51:19.933: D/dalvikvm(5401): GC_FOR_MALLOC freed 6030 objects / 254136 bytes in 79ms
06-01 04:51:21.273: W/IInputConnectionWrapper(5401): showStatusIcon on inactive InputConnection
06-01 04:51:22.813: D/dalvikvm(5401): GC_EXTERNAL_ALLOC freed 797 objects / 47568 bytes in 45ms
06-01 04:51:22.884: D/AndroidRuntime(5401): Shutting down VM
06-01 04:51:22.884: W/dalvikvm(5401): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-01 04:51:22.903: E/AndroidRuntime(5401): FATAL EXCEPTION: main
06-01 04:51:22.903: E/AndroidRuntime(5401): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/1 }} to activity {com.example.fatwallet/com.example.fatwallet.Create_adds_Activity}: java.lang.NullPointerException
06-01 04:51:22.903: E/AndroidRuntime(5401):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
06-01 04:51:22.903: E/AndroidRuntime(5401):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
06-01 04:51:22.903: E/AndroidRuntime(5401):     at android.app.ActivityThread.access$2800(ActivityThread.java:125)
06-01 04:51:22.903: E/AndroidRuntime(5401):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
06-01 04:51:22.903: E/AndroidRuntime(5401):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 04:51:22.903: E/AndroidRuntime(5401):     at android.os.Looper.loop(Looper.java:123)
06-01 04:51:22.903: E/AndroidRuntime(5401):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-01 04:51:22.903: E/AndroidRuntime(5401):     at java.lang.reflect.Method.invokeNative(Native Method)
06-01 04:51:22.903: E/AndroidRuntime(5401):     at java.lang.reflect.Method.invoke(Method.java:521)
06-01 04:51:22.903: E/AndroidRuntime(5401):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-01 04:51:22.903: E/AndroidRuntime(5401):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-01 04:51:22.903: E/AndroidRuntime(5401):     at dalvik.system.NativeStart.main(Native Method)
06-01 04:51:22.903: E/AndroidRuntime(5401): Caused by: java.lang.NullPointerException
06-01 04:51:22.903: E/AndroidRuntime(5401):     at com.example.fatwallet.Create_adds_Activity.onActivityResult(Create_adds_Activity.java:109)
06-01 04:51:22.903: E/AndroidRuntime(5401):     at android.app.Activity.dispatchActivityResult(Activity.java:3890)
06-01 04:51:22.903: E/AndroidRuntime(5401):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
06-01 04:51:22.903: E/AndroidRuntime(5401):     ... 11 more

1 个答案:

答案 0 :(得分:0)

听起来你得到的是“App没有响应”或ANR错误。这意味着您在UI线程上做了太多工作。要解决此问题,您需要创建一个新线程或使用AsyncTask来执行图像加载。