0

Android で Base 64 を使用せずに Web サービスを使用して画像 (サイズが小さい場合も大きい場合もあります) を送信するにはどうすればよいですか?

私はBase 64で作業しており、サーバーに画像を表示できました。

AndroidでWebサービスを使用して画像を送信するためにBase 64を使用する以外に可能性はありますか?

4

2 に答える 2

0

..そして私はApache libを使用しました。

私のスレッドで 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;
        }

そして、これが投稿方法です。

 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();
    }
}    
于 2013-05-27T11:40:15.813 に答える
0
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) {
}
于 2013-05-27T11:28:12.417 に答える