简体   繁体   English

这是在android中将多个图像上传到服务器的最佳方式

[英]Which is the best way to upload multiple images to the server in android

I am using okHttp to upload multiple images(more than 10 in this case) to the server using multipartbody.我正在使用 okHttp 使用 multipartbody 将多个图像(在这种情况下超过 10 个)上传到服务器。 I and my friend had argument, I am saying to upload all images in a single request.我和我的朋友有过争论,我是说在一个请求中上传所有图像。 He is saying send one request at a time once the previous image is uploaded upload next one.他是说,一旦上一张图片上传上传下一张,一次发送一个请求。 Which is the right thing to do, so the server works fast and no timeout occurs.这是正确的做法,因此服务器运行速度快且不会发生超时。

You can send Base64 format (String) like below and create one text file that contains all encoded photo as string您可以发送如下所示的 Base64 格式(字符串)并创建一个包含所有编码照片作为字符串的文本文件

/**
     * Encodes the image to Base64.
     */
    private String encodeImage(String photoPath) {

        File imagefile = new File(photoPath);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(imagefile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        Bitmap bm = BitmapFactory.decodeStream(fis);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 80, baos);
        byte[] b = baos.toByteArray();
        return Base64.encodeToString(b, Base64.DEFAULT);
    }

and use MultipartUtility to upload file:并使用 MultipartUtility 上传文件:

https://github.com/cloudinary/cloudinary_android/blob/master/Cloudinary/src/com/cloudinary/MultipartUtility.java https://github.com/cloudinary/cloudinary_android/blob/master/Cloudinary/src/com/cloudinary/MultipartUtility.java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM