创建图片上传的Android问题

时间:2013-12-23 03:57:11

标签: android

我想弄清楚这里是不对的,我正在为用户构建一个表单,以便能够上传个人资料图片。当点击按钮它会带你到Android画廊(想让用户在那里拍照)但是一旦选择了一张图片,应用程序崩溃不确定为什么,但我认为错误是指向与getRealPathFromURI(currImageURI)下面的方法我试过寻找不同的解决方案,但它们似乎对如何做到这一点有相同的反应......就像这样How to get the Full file path from URI http://monstercoda.wordpress.com/2012/04/15/android-image-upload-tutorial-part-i/

代码:

@Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if(resultCode == RESULT_OK) {
                if(requestCode == 1) {
                              Uri currImageURI = data.getData();
                    Log.d("The path","Current image path is ---->" + getRealPathFromURI(currImageURI));
                    TextView tv_path = (TextView) findViewById(R.id.path);
                    tv_path.setText(getRealPathFromURI(currImageURI));

                }
            }
        }

        public String getRealPathFromURI(Uri contentUri) {
            String res = null;
            String[] proj={MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(contentUri,proj, null, null, null);
            if(cursor.moveToFirst()) {
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                res = cursor.getString(column_index);
            }
            cursor.close();
            return res;
        }

错误:

12-22 16:25:38.894: E/AndroidRuntime(19577): FATAL EXCEPTION: main
12-22 16:25:38.894: E/AndroidRuntime(19577): Process: com.fslio, PID: 19577
12-22 16:25:38.894: E/AndroidRuntime(19577): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.google.android.apps.docs.storage/document/acc=1;doc=172 flg=0x1 }} to activity {com.fslio/com.fslio.wardrobe}: java.lang.IllegalArgumentException: Unknown column requested: _data
12-22 16:25:38.894: E/AndroidRuntime(19577):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3346)
12-22 16:25:38.894: E/AndroidRuntime(19577):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3389)
12-22 16:25:38.894: E/AndroidRuntime(19577):    at android.app.ActivityThread.access$1200(ActivityThread.java:135)
12-22 16:25:38.894: E/AndroidRuntime(19577):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1445)
12-22 16:25:38.894: E/AndroidRuntime(19577):    at android.os.Handler.dispatchMessage(Handler.java:102)
12-22 16:25:38.894: E/AndroidRuntime(19577):    at android.os.Looper.loop(Looper.java:137)
12-22 16:25:38.894: E/AndroidRuntime(19577):    at android.app.ActivityThread.main(ActivityThread.java:4998)
12-22 16:25:38.894: E/AndroidRuntime(19577):    at java.lang.reflect.Method.invokeNative(Native Method)
12-22 16:25:38.894: E/AndroidRuntime(19577):    at java.lang.reflect.Method.invoke(Method.java:515)
12-22 16:25:38.894: E/AndroidRuntime(19577):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-22 16:25:38.894: E/AndroidRuntime(19577):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-22 16:25:38.894: E/AndroidRuntime(19577):    at dalvik.system.NativeStart.main(Native Method)
12-22 16:25:38.894: E/AndroidRuntime(19577): Caused by: java.lang.IllegalArgumentException: Unknown column requested: _data
12-22 16:25:38.894: E/AndroidRuntime(19577):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:167)
12-22 16:25:38.894: E/AndroidRuntime(19577):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
12-22 16:25:38.894: E/AndroidRuntime(19577):    at android.content.ContentProviderProxy.query(ContentProviderNative.java:413)
12-22 16:25:38.894: E/AndroidRuntime(19577):    at android.content.ContentResolver.query(ContentResolver.java:461)
12-22 16:25:38.894: E/AndroidRuntime(19577):    at android.content.ContentResolver.query(ContentResolver.java:404)
12-22 16:25:38.894: E/AndroidRuntime(19577):    at com.fslio.wardrobe.getRealPathFromURI(wardrobe.java:80)
12-22 16:25:38.894: E/AndroidRuntime(19577):    at com.fslio.wardrobe.onActivityResult(wardrobe.java:67)
12-22 16:25:38.894: E/AndroidRuntime(19577):    at android.app.Activity.dispatchActivityResult(Activity.java:5435)
12-22 16:25:38.894: E/AndroidRuntime(19577):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3342)
12-22 16:25:38.894: E/AndroidRuntime(19577):    ... 11 more

1 个答案:

答案 0 :(得分:4)

是,问题在getRealPathFromURI inside the onActivityResult

错误是,

Caused by: java.lang.IllegalArgumentException: Unknown column requested: _data
com.fslio.wardrobe.getRealPathFromURI(wardrobe.java:80)
com.fslio.wardrobe.onActivityResult(wardrobe.java:67)

调试代码,在选择图像时,数据会出现问题。

有关详细信息,请查看this参考链接。