簡體   English   中英

從Android上傳到服務器時,Base64編碼的圖像會被截斷

[英]Base64 encoded image gets cut-off when uploading from android to server

我在使用put-volley-request向運行php / slim的服務器發送base64編碼的圖像時遇到一些問題。

當我在發送之前輸出數據時看起來不錯,但是一旦我在服務器上將其獲取,數據就會被破壞/切斷。

我的用於發送圖像的Android代碼如下所示:

private void updateImage(Bitmap myImage) {
    String tag_string_req = "req_updateimage";

    String updateUrl = String.format(API_DOMAIN + AppConfig.URL_UPDATE_IMAGE);
    StringRequest strReq = new StringRequest(Request.Method.PUT, updateUrl, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            try {
                //do some stuff
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Error: " + error.getMessage());
        }

    }){

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Content-Type", "application/json; charset=UTF-8");

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            myImage.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
            byte[] byteArray = byteArrayOutputStream.toByteArray();
            String base64Image = Base64.encodeToString(byteArray, Base64.DEFAULT);
            params.put("image", base64Image);

            System.out.println(params);
            return params;
        }
    };

    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}

在我的服務器上,我有一個類似的功能:

$app->put('/image', function() use ($app){
    $image = $app->request->put('image');
    $response['error'] = false;
    $response['image'] = $image;
    echoResponse(200, $response);
});

我的android-function中的輸出看起來像一個正確的base64編碼圖像。 在我的服務器上,整個圖像僅占大約30%。 你能幫助我嗎? 我的蟲子在哪里? 怎么了?

提前致謝,

P

您似乎未設置正確的內容類型。

在您的Java代碼中...

params.put(“ Content-Type”,“ application / json; charset = UTF-8”);

它告訴您的苗條應用程序您要發送的數據是以application / json格式...不是。

嘗試更改編碼以匹配要發送的數據結構。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM