如何使用多部分形式的数据JSON发送图像(位图)到服务器的android [英] how to send image (bitmap) to server in android with multipart-form data json

查看:130
本文介绍了如何使用多部分形式的数据JSON发送图像(位图)到服务器的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code图像上传到服务器和它的作品,

  HttpEntity resEntity;

                HttpClient的HttpClient的=新DefaultHttpClient();
                HttpPost后=新HttpPost(Constants.url_create_product);
                MultipartEntity reqEntity =新MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                档案文件=新的文件(路径);
                FileBody斌=新FileBody(文件);

                reqEntity.addPart(手机,新StringBody(mPhoneNumber));
                reqEntity.addPart(prod_title,新StringBody(namapro));
                reqEntity.addPart(prod_price,新StringBody(hargapro));
                reqEntity.addPart(prod_desc,新StringBody(despro));
                reqEntity.addPart(prod_order,新StringBody(orderpro));
                reqEntity.addPart(prod_image,斌);

                post.setEntity(reqEntity);
                    HTT presponse响应= httpClient.execute(后);
                resEntity = response.getEntity();
                字符串response_str = EntityUtils.toString(resEntity);
                GSON GSON =新GSON();
                gson.toJson(response_str);
                 如果(resEntity!= NULL){
                     Log.i(响应,response_str);
                     runOnUiThread(新的Runnable(){
                            公共无效的run(){
                                 尝试 {
                                    Toast.makeText(getApplicationContext(),上传完成检查服务器的上传目录。,Toast.LENGTH_LONG).show();
                                }赶上(例外五){
                                    e.printStackTrace();
                                }
                               }
                        });
                 }
 

但我有菜单图像编辑器。该编辑器是作物的形象和多数民众赞成在code返回位图这样的值

 捆绑额外= data.getExtras();

                如果(临时演员!= NULL){
                    照片= extras.getParcelable(数据);

                    mImageView.setImageBitmap(照片);
                }

                文件F =新的文件(mImageCaptureUri.getPath());

                如果(f.exists())f.delete();

                打破;
 

我想请问,怎么解决方案发送图像服务器参数位图。正如你知道我的code立即发送图像使用参数路径(字符串)。

解决方案

  ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
photo.com preSS(Bitmap.Com pressFormat.JPEG,100,BAOS);
byte []的imageBytes = baos.toByteArray();
字符串连接codeDIMAGE = Base64.en codeToString(imageBytes,Base64.DEFAULT);
 

然后发送该连接codeDIMAGE为字符串和你的服务器,你需要去code是为了得到图像本身。

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).

解决方案

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 String and in your server you will need to decode it in order to get the image itself.

这篇关于如何使用多部分形式的数据JSON发送图像(位图)到服务器的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆