如何使用多部分格式数据json将图像(位图)发送到android中的服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12796579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 12:06:23  来源:igfitidea点击:

how to send image (bitmap) to server in android with multipart-form data json

android

提问by ya Lya

I have code to upload image to server and it works ,

我有将图像上传到服务器的代码并且它可以工作,

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();
                                }
                               }
                        });
                 }

But i have menu image editor . that editor is crop image and thats code return bitmap value like this

但我有菜单图像编辑器。该编辑器是裁剪图像,代码返回位图值,如下所示

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;

I wanna ask , how solution to send image to server with parameter bitmap. As you know my code to send image now use parameter path (string).

我想问一下,如何使用参数位图将图像发送到服务器的解决方案。如您所知,我现在发送图像的代码使用参数路径(字符串)。

回答by Carnal

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

Then send this encodedImage as a Stringand in your server you will need to decode it in order to get the image itself.

然后将此编码图像作为 a 发送String,在您的服务器中,您需要对其进行解码才能获得图像本身。

回答by Pradeep Bishnoi

*use three library
1.httpclient-4.3.6
2.httpcore-4.3.3
3.httpmime-4.3.6

*使用三个库
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();
            }