簡體   English   中英

這是在android中將多個圖像上傳到服務器的最佳方式

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

我正在使用 okHttp 使用 multipartbody 將多個圖像(在這種情況下超過 10 個)上傳到服務器。 我和我的朋友有過爭論,我是說在一個請求中上傳所有圖像。 他是說,一旦上一張圖片上傳上傳下一張,一次發送一個請求。 這是正確的做法,因此服務器運行速度快且不會發生超時。

您可以發送如下所示的 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);
    }

並使用 MultipartUtility 上傳文件:

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