【问题标题】:Sending multiple images to the server from android从android发送多个图像到服务器
【发布时间】:2013-12-09 04:35:48
【问题描述】:

我正在尝试使用 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();
        }

我的问题 :: 我是按照正确的方法传递上面的多张图片还是有更好的方法来做到这一点?

【问题讨论】:

标签: android multipartentity


【解决方案1】:

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

首先,您的视图在下载/上传时不会冻结。多个文件将被异步午餐。你可以像下面这样简单地调用它:

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

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

希望我对你有所帮助。 祝你好运

【讨论】:

  • @AndroidNabz这是一个简单的事情如何在单个api上上传两种类型的数据......上传字符串和图像的单一方法如果你知道它怎么能Dun?
【解决方案2】:

我的做法:

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

并将图像作为字符串发送

在服务器中(使用 Slim 框架):

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多