从android发送多个图像到服务器

时间:2013-12-09 04:35:49

标签: android multipartentity

我试图使用multipart

将多个值从android发布到服务器

我做了什么 - 我能够将一个图像(从android drawable)发送到服务器。

我现在的代码:

try
        {
            Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher); 

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost("http://10.0.2.2:7002/Details/");
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            try{
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bitmapOrg.compress(CompressFormat.JPEG, 75, bos);
                byte[] data = bos.toByteArray();
                ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
                reqEntity.addPart("key", bab);
            }
            catch(Exception e){
                //Log.v("Exception in Image", ""+e);
                reqEntity.addPart("picture", new StringBody(""));
            }
            postRequest.setEntity(reqEntity);       
            HttpResponse response = httpClient.execute(postRequest);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
            String sResponse;
            StringBuilder s = new StringBuilder();
            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }
        }catch(Exception e){
            e.getStackTrace();
        }

如何发送两张图片 :: 这是我提出的模型

try
        {
                        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
                        Bitmap bitmapOrg2 = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher2);   

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new        HttpPost("http://10.0.2.2:7002/Details/");
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            try{
                ByteArrayOutputStream bos = new ByteArrayOutputStream();

                                bitmapOrg.compress(CompressFormat.JPEG, 75, bos);       
                                bitmapOrg2.compress(CompressFormat.JPEG, 75, bos);

                                byte[] data = bos.toByteArray();

                    ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
                                ByteArrayBody bab2 = new ByteArrayBody(data, "earth.jpg");

                                reqEntity.addPart("key", bab);
                                reqEntity.addPart("key1", bab2);
            }
            catch(Exception e){
                //Log.v("Exception in Image", ""+e);
                reqEntity.addPart("picture", new StringBody(""));
            }
            postRequest.setEntity(reqEntity);       
            HttpResponse response = httpClient.execute(postRequest);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
            String sResponse;
            StringBuilder s = new StringBuilder();
            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }
        }catch(Exception e){
            e.getStackTrace();
        }

我的问题 ::我是否按照正确的方式传递了多张图片,或者有更好的方法吗?

2 个答案:

答案 0 :(得分:1)

是的,有更好的方法可以上传或下载多个文件。 首先,你必须定义一个AsyncTask,在他的 doInBackground 函数中,你必须编写代码来上传/下载文件。

由此,首先您的视图在下载/上传时不会冻结。多个文件将异步启动。你可以像下面这样简单地称之为:

new MyAsyncTask(fileUrl1).execute();
new MyAsyncTask(fileUrl2).execute();

这是一种更好,更易于管理的方法,可以使用HTTP请求。

希望我帮助过你。 祝你好运

答案 1 :(得分:0)

我这样做的方法:

public String getStringImage(Bitmap bmp) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 40, baos);
        byte[] imageBytes = baos.toByteArray();
        return Base64.encodeToString(imageBytes, Base64.DEFAULT);
    }

并将图像作为String

发送

在服务器中(使用Slim Framework):

$image = $app->request()->post('img_p');

file_put_contents($my_path, base64_decode($image));

我不确定这是更好的方法,但它有效。

相关问题