如何在android中使用multipart-form数据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();
                                }
                               }
                        });
                 }

我有菜单图像编辑器.该编辑器是裁剪图像,并且该代码返回位图值

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;

我想问,如何使用参数位图将图像发送到服务器.你知道我的代码发送图像现在使用参数路径(字符串).

解决方法

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

然后发送这个encodedImage作为一个字符串,并在您的服务器您将需要解码它,以获得图像本身.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...