1

我正在尝试将我上传到 android 应用程序中的 imageview 的图像发送到服务器以上传到那里。我已经在这里和那里阅读了一些教程,但是我尝试过的方法不起作用,所以这可能是由于服务器处理图片的方式造成的。

好吧,这是我如何将图片获取到 android 中的 imageview 的代码

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();
            image = (ImageView) findViewById(R.id.yprofilepic);
            image.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            Bitmap bitmap = BitmapFactory.decodeFile(picturePath);           
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
            byte [] byte_arr = stream.toByteArray();
            image_str = Base64.encodeBytes(byte_arr);

}

这是在上传之前在我的服务器上接收图片的方式。

public function upload_picture(){
            $picture = (isset($_POST["picture"])) ? $_POST["picture"] : "";

             $picture = "";

                if(isset($_FILES) && isset($_FILES["picture"]))
                {
                    $picture_file = $_FILES["picture"];
                    if($picture_file["size"] > 0 && !empty($picture_file["name"]))
                    {
                        $extension = explode(".", $picture_file["name"]);
                        $extension = $extension[sizeof($extension)-1];
                        $file_extensions = array('jpg', 'jpeg', 'gif', 'bmp', 'png');
                        if(in_array($extension, $file_extensions))
                        {
                            $new_file = "assets/uploads/".md5(time().rand(0, 1000).$email).".".$extension;
                            if (move_uploaded_file($picture_file['tmp_name'], $new_file)) {
                                $image = new Image_Library();
                                $image->load($new_file);
                                $image->resizeToWidth(150);
                                $image->resizeToHeight(150);
                                $image->save($new_file);
                                $picture = SITE_ROOT.$new_file;
                            }
                        }
                    }
                }

                $model = new Users_Model();
                $status = $model->upload_picture($user_id, $picture);
                $response = array("operation" => $status);
                return new JSON($response);

            }

我不知道使用 POST 请求将图像从 android 发送到服务器的最佳方式是什么。

编辑我实际上使用了 coderzheaven 方法,但它没有用。这就是我实现它的方式。

class uploadPic extends AsyncTask<String, String, String>{

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            System.out.println("This is the string for the picture "+image_str);
            List<NameValuePair> p=new ArrayList<NameValuePair>();
            p.add(new BasicNameValuePair("username",user));
            JSONObject js=jsonParser.makeHttpRequest(url_id, "POST", p);

                 ArrayList<NameValuePair> nameValuePairs = new  ArrayList<NameValuePair>();

            nameValuePairs.add(new BasicNameValuePair("picture",image_str));
            try {
                nameValuePairs.add(new BasicNameValuePair("username", js.getString("user_id")));
            } catch (JSONException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            JSONObject j=jsonParser.makeHttpRequest(upload_pic, "POST", nameValuePairs);
            Log.d("Picture status", j.toString());
            return null;
        }

    }
4

2 回答 2

0

我认为一个不错的选择是发送图像的 base 64 编码字符串,而不是图像文件。这样您就可以发送 POST 请求并将文件保存在服务器中。

于 2013-07-29T11:32:05.493 回答
0

我可以告诉你这些步骤,但你必须尝试它的确切代码

脚步

1) 从 ImageView 中检索位图对象(使用方法 iv.getDrawingCache())

2)从该位图对象中获取您的 byte[]

3)将字节转换为base64string如下( String encoded = Base64.encodeToString(bytes, 0);

4)将此base64string作为参数传递给webservice。

而已

于 2013-07-29T12:10:57.963 回答