2013-07-29 59 views
1

我试图发送一个图像,我上传到android应用程序中的图像视图到服务器上传到那里。我在这里和那里阅读了一些教程,但是我所尝试的并不是可行的,所以可能是由于服务器处理图片的方式。通过发布请求发送从Android imagevview到服务器的图像

那么这里就是我如何获得Android的

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); 

} 

这就是图片被上传前,我的服务器上接收的画面到ImageView的代码。

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); 

      } 

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

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

    } 
+0

http://www.coderzheaven.com/2011/04/25/android-upload-an-image-to-a-server/ – KOTIOS

+0

与邮差测试服务器,然后检查是否问题在Android或服务器代码中。 – Ayush

回答

0

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

+0

检查编辑。我以这种方式实施,但它没有奏效。 –

0

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

STEPS

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

2)从位图对象

3得到您的byte [])转换成字节作为base64string遵循 (String encoded = Base64.encodeToString(bytes,0);

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

完蛋了