通过Post请求将图像从Android imagevview发送到服务器

时间:2013-07-29 11:28:08

标签: php android image post

我正在尝试将我上传到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发送到服务器的最佳方法是什么。

EDIT 我实际上使用了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;
        }

    }

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

<强> STEPS

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

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

3)将字节转换为base64string,如下所示     ( 字符串编码= Base64.encodeToString(bytes,0);

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

多数民众赞成

相关问题