【问题标题】:How can I upload and send Images to server using webservices without using Base 64 encoded string in Android?如何在不使用 Android 中的 Base64 编码字符串的情况下使用 Web 服务将图像上传和发送到服务器?
【发布时间】:2013-05-22 05:57:05
【问题描述】:

如何在 Android 中使用 Web 服务而不使用 Base 64 发送图像(可能是小尺寸或大尺寸)?

我曾在 Base 64 上工作过,可以在服务器中显示图像。

除了使用 Base 64 在 Android 中使用 web 服务发送图像之外,还有其他可能吗?

【问题讨论】:

  • 可以使用Apache公共库上传图片。
  • @Nasser- web 服务中的 Apache 通用库 ??
  • 实际上我不记得那个包的名称,但它们在我的项目 libs 文件夹中为 httpclient-4.2.3.jar、httpcore-4.2.2.jar、httpmime-4.2.3.jar

标签: php android image web-services base64


【解决方案1】:
try {
      final ByteArrayOutputStream out = new ByteArrayOutputStream();
      File file = new File(path_to_your_image);
      final InputStream in = new FileInputStream(file);
        final byte[] buf = new byte[2048];
        int n;
        while ((n = in.read(buf)) >= 0) {
          out.write(buf, 0, n);
        }
      final byte[] data = out.toByteArray();
      String urlString = "http://ip_address/your_php";
      HttpPost postRequest = new HttpPost(urlString);
      postRequest.setEntity(new  ByteArrayEntity(data));
      HttpClient client = new DefaultHttpClient();
      HttpResponse response = client.execute(postRequest);
} catch(Exception e) {
}

【讨论】:

    【解决方案2】:

    ..我使用 apache lib as.

    在我的线程中 doInBackground() 方法:

    @Override
            protected Object doInBackground(Object... params) {                    
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("photo", photosURLString));
                LogMsg.d("Photo "+photosURLString);
                if (loactionID != null){
                    nameValuePairs.add(new BasicNameValuePair("LocationID", loactionID));
                    LogMsg.d("teamID "+loactionID);
                }
                if (businessID != null){
                    nameValuePairs.add(new BasicNameValuePair("BusinessID", businessID));
                    LogMsg.d("BusinesID "+businessID);
                }
                nameValuePairs.add(new BasicNameValuePair("UserID", userID));
                LogMsg.d("userID "+userID);
    
                post(ActivityUploadPhotos.this.getString(R.string.image_upload), nameValuePairs);
    
                return null;
            }
    

    这里是 post 方法。

     public void post(String url, List<NameValuePair> nameValuePairs) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(url);
        try {
            MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    
            for(int index=0; index < nameValuePairs.size(); index++) {
                if(nameValuePairs.get(index).getName().equalsIgnoreCase("photo")) {
                    // If the key equals to "image", we use FileBody to transfer the data
                    entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue()) ,"image/jpeg" ));
                } else {
                    // Normal string data
                    entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
                }
            }
            httpPost.setEntity(entity);
    
            HttpResponse response = httpClient.execute(httpPost, localContext);
            LogMsg.d(""+response);
    
        } catch (IOException e) {
            e.printStackTrace();
        }
    }    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-18
      • 2016-10-23
      • 1970-01-01
      相关资源
      最近更新 更多