Android: Sending an image through POST

前端 未结 1 1417
慢半拍i
慢半拍i 2021-01-15 11:45

I\'ve been searching around for the solution, and have come across the multipart and different setups, but I can\'t seem to get it working correctly.

Here\'s what I

相关标签:
1条回答
  • 2021-01-15 12:31

    This is what I did yesterday, maybe it will help

            Bitmap bitmapOrg = images.get(0);
    
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
    
            String upload_url = prepare_upload_url();
            bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
    
            byte[] data = bao.toByteArray();
    
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost(upload_url);
            MultipartEntity entity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);
    
            //Set Data and Content-type header for the image
            entity.addPart("file",
                    new ByteArrayBody(data, "image/jpeg", "file"));
            postRequest.setEntity(entity);
            try {
    
                HttpResponse response = httpClient.execute(postRequest);
            //Read the response
                String jsonString = EntityUtils.toString(response.getEntity());
                Log.v(ProgramConstants.TAG, "after uploading file "
                        + jsonString);
    
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
    0 讨论(0)
提交回复
热议问题