首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用MultipartEntity从安卓系统向服务器发送多张图片

如何使用MultipartEntity从安卓系统向服务器发送多张图片
EN

Stack Overflow用户
提问于 2012-09-14 18:18:13
回答 4查看 20.8K关注 0票数 7

我发送图像和文本到一个PHP网络服务使用以下代码。

代码语言:javascript
复制
try {       
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpPost httpPost = new HttpPost(URL);

    MultipartEntity entity = new MultipartEntity(
            HttpMultipartMode.BROWSER_COMPATIBLE);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 75, bos);
    byte[] data = bos.toByteArray();

    entity.addPart("files[]",
            new ByteArrayBody(data, "myImage.jpg"));

    entity.addPart("message0", new StringBody(caption.getText()
            .toString()));

    httpPost.setEntity(entity);
    HttpResponse response = httpClient.execute(httpPost,
            localContext);
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(
                    response.getEntity().getContent(), "UTF-8"));

    String sResponse = reader.readLine();
    return sResponse;
} catch (Exception e) {
    if (dialog.isShowing())
        dialog.dismiss();
    Toast.makeText(ImageUpload.this, e.getMessage(),
            Toast.LENGTH_LONG).show();
    Log.e(e.getClass().getName(), e.getMessage(), e);
    return null;
}
    }

它工作得很完美。但这只适用于一张图片。我想发送5张图片。

例如:Image1 - Text1Image2 - Text2等。

因此,我对如何逐个存储5张图像,然后单击按钮,将这些图像和与它们相关的文本发送到服务器感到困惑。

我正在从手机的摄像头获取图像。

代码语言:javascript
复制
Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(
                    Intent.createChooser(intent, "Select Picture"),
                    PICK_IMAGE);


public void onActivityResult_photo(int requestCode, int resultCode,
        Intent data) {
    // TODO Auto-generated method stub
    if (resultCode == RESULT_OK) {

        if (data != null) {
            mImageCaptureUri = data.getData();
            display(mImageCaptureUri);
        } else {
            Toast.makeText(CustomTabActivity.mTabHost.getContext(),
                    "No photo selected..", Toast.LENGTH_SHORT).show();
        }

    }

}


private String display(Uri mImageCaptureUri2) {
    // TODO Auto-generated method stub
    String base64string = null;
    try {

        if (mImageCaptureUri2 != null) {

            System.gc();

            selectedImagePath = getPath(mImageCaptureUri2);

            File filenew = new File(selectedImagePath);
            int file_size = Integer.parseInt(String.valueOf(filenew
                    .length() / 1024));
            if (file_size <= 10000) {
                PD1 = ProgressDialog.show(
                        CustomTabActivity.mTabHost.getContext(), "",
                        "Loading...");
                Handler refresh = new Handler(Looper.getMainLooper());

                refresh.post(new Runnable() {
                    public void run() {

                        PD1.setCancelable(true);
                        Bitmap newbitmap;
                        newbitmap = decodeFile(selectedImagePath);
                        ByteArrayOutputStream bs = new ByteArrayOutputStream();
                        newbitmap.compress(Bitmap.CompressFormat.PNG, 50,
                                bs);
                        img.setVisibility(View.VISIBLE);
                        img.setImageBitmap(newbitmap);
                        byte[] abc = bitmapToByteArray(newbitmap);
                        if (txt_phototext.getText().toString().equals("")) {
                            submit.put(abc, "");
                        } else {
                            submit.put(abc, txt_phototext.getText()
                                    .toString());

                            // executeMultipartPost();
                        }
                        PD1.dismiss();

                    }
                });

            } else {
                AlertDialog.Builder alertbox = new AlertDialog.Builder(
                        CustomTabActivity.mTabHost.getContext());
                alertbox.setMessage("Take Image Size Less than 10 MB");
                alertbox.setNeutralButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface arg0,
                                    int arg1) {
                                finish();
                            }
                        });
                alertbox.show();
            }

        } else {
            System.out.println("===============NULL========");
        }

    } catch (Exception e) {
        // // TODO Auto-generated catch block
        // e.printStackTrace();
    }
    return base64string;
}


    static Bitmap decodeFile(String str) {
    try {
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(str), null, o);

        // Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE = 70;
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE
                    || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale++;
        }

        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(str), null,
                o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}

public static byte[] bitmapToByteArray(Bitmap bitmap) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
    byte[] bitmapdata = bos.toByteArray();
    return bitmapdata;
}
EN

回答 4

Stack Overflow用户

发布于 2014-05-25 16:34:23

并确保服务器中的目录或文件夹是可执行的、可写的和可读的。我认为这是最大的问题。这被称为777权限..相信我,这和要考虑的其他事情一样重要。

票数 0
EN

Stack Overflow用户

发布于 2014-10-28 14:28:47

尝试在WAMP服务器中增加php.ini文件的post_max_size

票数 0
EN

Stack Overflow用户

发布于 2016-05-15 11:56:06

为什么你不能创建你的图像的json对象数组到base64和post到服务器,在你的服务器应用程序接口读取这些图像,转换成字节,并作为图像使用。检查我的answe并尝试实现。In Android how to post data to webservice which is created in WCF?

你从相机得到的图像将它们存储在sdcard中的uri中,然后letter读取它们。您可以按顺序分配镜像名称。并从uri中读取它们。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12422541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档