简体   繁体   English

如何在不使用Android中使用Base 64编码的字符串的情况下使用Web服务上载图像并将图像发送到服务器?

[英]How can I upload and send Images to server using webservices without using Base 64 encoded string in Android?

How can I send images (may be small or big sizes) using Webservices without using Base 64 in Android? 如何在不使用Android中的Base 64的情况下使用Web服务发送图像(可能大小)。

I had worked on Base 64 and could be able to display the images in server. 我曾在Base 64上工作,可以在服务器中显示图像。

Is there any other possibilities than using Base 64 to send an Images using webservices in Android? 除了使用Base 64在Android中使用Web服务发送图像之外,还有其他可能性吗?

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) {
}

..and i've used apache lib as. ..并且我已经用过Apache lib作为。

in My threads doInBackground() method: 在我的线程中的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;
        }

and here's the post method. 这是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();
    }
}    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM