2012-10-09 25 views
9

tôi có mã để tải lên hình ảnh đến máy chủ và nó hoạt động,làm thế nào để gửi hình ảnh (bitmap) đến máy chủ trong Android với nhiều phần dữ liệu dạng dữ liệu json

HttpEntity resEntity; 

       HttpClient httpClient = new DefaultHttpClient(); 
       HttpPost post = new HttpPost(Constants.url_create_product); 
       MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
       File file= new File(path); 
       FileBody bin = new FileBody(file); 

       reqEntity.addPart("phone", new StringBody(mPhoneNumber)); 
       reqEntity.addPart("prod_title", new StringBody(namapro)); 
       reqEntity.addPart("prod_price", new StringBody(hargapro)); 
       reqEntity.addPart("prod_desc", new StringBody(despro)); 
       reqEntity.addPart("prod_order", new StringBody(orderpro)); 
       reqEntity.addPart("prod_image", bin); 

       post.setEntity(reqEntity); 
        HttpResponse response = httpClient.execute(post); 
       resEntity = response.getEntity(); 
       String response_str = EntityUtils.toString(resEntity); 
       Gson gson = new Gson(); 
       gson.toJson(response_str); 
       if (resEntity != null) { 
        Log.i("RESPONSE",response_str); 
        runOnUiThread(new Runnable(){ 
          public void run() { 
           try { 
            Toast.makeText(getApplicationContext(),"Upload Complete. Check the server uploads directory.", Toast.LENGTH_LONG).show(); 
           } catch (Exception e) { 
            e.printStackTrace(); 
           } 
           } 
         }); 
       } 

Nhưng tôi có chỉnh sửa ảnh đơn. trình chỉnh sửa đó là hình cắt và mã trả về bitmap giá trị như thế này

Bundle extras = data.getExtras(); 

       if (extras != null) {    
        photo = extras.getParcelable("data"); 

        mImageView.setImageBitmap(photo); 
       } 

       File f = new File(mImageCaptureUri.getPath());    

       if (f.exists()) f.delete(); 

       break; 

Tôi muốn hỏi, cách gửi hình ảnh đến máy chủ với bitmap tham số. Như bạn biết mã của tôi để gửi hình ảnh bây giờ sử dụng đường dẫn tham số (chuỗi).

Trả lời

29
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
byte[] imageBytes = baos.toByteArray(); 
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT); 

Sau đó gửi encodedImage này như một String và trong máy chủ của bạn, bạn sẽ cần phải giải mã nó để có được hình ảnh riêng của mình.

+0

xin lỗi, newbie iam, vậy làm thế nào để máy chủ giải mã nó để có được hình ảnh? –

+0

phụ thuộc vào ngôn ngữ bạn đang sử dụng trên máy chủ của mình. PHP đã xây dựng phương thức cho việc này. Chỉ cần google ngôn ngữ trên máy chủ + giải mã chuỗi hình ảnh. – Carnal

+0

Không vấn đề gì cả! – Carnal

4

* sử dụng ba thư viện
1.httpclient-4.3.6
2.httpcore-4.3.3
3.httpmime-4.3.6

  byte[] data = null; 
      try {  
       String url = "http://andapp.freetings.in/testbyme.php?"; 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(url); 
       MultipartEntity entity = new MultipartEntity(); 

       if(imageBitmap!=null){ 
        ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
        imageBitmap.compress(CompressFormat.JPEG, 100, bos); 
        data = bos.toByteArray(); 
        entity.addPart("uplod_img", new ByteArrayBody(data,"image/jpeg", "test2.jpg")); 
       } 
       // entity.addPart("category", new StringBody(categoryname,"text/plain",Charset.forName("UTF-8"))); 
       entity.addPart("category", new StringBody(catid,"text/plain",Charset.forName("UTF-8"))); 
       entity.addPart("your_contact_no", new StringBody(phone,"text/plain",Charset.forName("UTF-8"))); 
       entity.addPart("your_emailid", new StringBody(email,"text/plain",Charset.forName("UTF-8"))); 
       entity.addPart("your_name", new StringBody(name,"text/plain",Charset.forName("UTF-8"))); 

       httppost.setEntity(entity); 
       HttpResponse resp = httpclient.execute(httppost);   
       HttpEntity resEntity = resp.getEntity(); 
       String string=EntityUtils.toString(resEntity); 
       // Log.e("sdjkfkhk", string); 
       return resEntity; 
      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
+0

tính năng này hoạt động với tôi –

+0

Không thể tìm thấy ByteArrayBody –

+0

u cần thêm ba tệp jar là httpclient-4.1.jar, httpcore-4.1.jar và httpmime-4.1.jar trong thư mục libs –

Các vấn đề liên quan