简体   繁体   中英

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. 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

/**
     * 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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