将图像发送到android中的python服务器

问题描述 投票:1回答:1

我正在尝试将图像发送到服务器。我从imageview获取图像,将其转换为位图,然后以字符串格式对其进行编码。

    profilePic = ( ImageView )findViewById(R.id.profilePic);

现在,将其转换为字符串格式:Bitmap bmap = profilePic.getDrawingCache();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    Bitmap resizedBitmap = Bitmap.createScaledBitmap(bmap, 150, 150, false);

    resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);

现在将它发送到AsyncTask:

new ReadFromWeb().execute("update", studentIdString, emailString,
            "http://*****/api/user/update/",
            firstNameString, middleNameString,
            lastNameString, motherNameString
            , fatherNameString,mobileNoString,encodedImage

    );

现在,在doInBackground中获取它:

 data += "&" + URLEncoder.encode("profile_pic", "UTF-8") + "="
                        + URLEncoder.encode(params[10],"UTF-8");

通过httpUrlconnection发送:

OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                Log.d(TAG, "SEnd" + data);
                wr.write(data);

图像是:

/ 9J / 4AAQSkZJRgABAQAAAQABAAD / 2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH / 2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH / wAARCACWAJYDASIA AhEBAxEB / 8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL / 8QAtRAAAgEDAwIEAwUFBAQA AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3 ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4 + Tl5ufo6erx8vP09fb3 + PN6 / 8QAHwEA AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL / 8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3 uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3 + PN6 / 9oADAMBAAIRAxEAPwDlf +次ngGez8R / CjxwiMbXWvAOiaNcSvBJGY7zS4VmhR9gWKQXVvqb + UNyyAQS / IWTNfClt8IfEp / Zx时/ Z7 / aI8Z / Fb9l74SeFv2jvFX7QmjfCfw / 8AEf4q / Ejw74n1G2 / Z5 + LviT4U + O9R8SakPgRc / d / w9FJr GiWt7pVhP49n1bUrLVrGKwtbm7ttXtYf2h + JF / BP / wCPelfsW + JfjL8X / wBoD4i6h4a + FPgyz8Z + FvCPxP8AhN4FtvF2r6hbWZsrTw9J4u8O / H7xqwikuLyKxsfER0OO2ubCezl04ahaPGT + CMT / SIFF H9rT9gf / AIIT + EfCvwO + LvxU + HNR + 0B / AMFK9A +的MO / DX4dfELxbongTw94p / wCChPiqzt77xvrv gzQdWTwXpV0lpfTf2trFxYQR2Njq16t2sFje3Ef4nUyTA0a / FGa5rlE8cqGDpZnhsLF1p4h2q1JV 40qeDxNN1pyjFKnFz5pTjFWje8v37L + LsbPLOEspyjPPqEXjcXlmMxcaOHjDkpYbnwaqyzHA1KdN Rlb2lSm9VNpyVnJ / Klx4C0nXJo7NP2qP2EZ7u8ngtrdB + 1N9onlnlkEUEEEZ + HYJkldkjjReWchQ BlmPZfED9jTxJ8FLvwUfiv8AtAfs6 + CLj4i + d / HV4 + 8PLq2h / tvTWzaF8ZYPFt38P7e + 1jQv2I9X 0a11jV5vBHi + xuNKbUWu9O1Hw9rdnfrHJApb60 / 4KY / 8EdvEPhDwb8GfiD + ZV + yr8TLPxB4N + IMO kX3w / wDgj + y18VvGGu + MvCmq6n4cvrfV / FepeDvD + qXlo3w7sdB1YrrniCx1HUfE9zrYsJtVXUob WKX9QfjL4c / aw0rWPgz4k0G6 / wCCrPhzwHo / 7N37Jfhub4afAv8AYtPj3wFovjTU / wC0fE817Jo + ufFX4dfFK18baXp / hrWdM + Iemar4Kh8H / DrUtT0bw38YrC4g8TyeBPED8PI8LcdZI82pcL4nK6E6 lagsLjo5tg8RGVDESpyaVXFL2tOqoKtGpRlOnyyjSnP20K1KPNx1xHxVwxiaGEw3F / 8AaE5WlOvQ p5HiKfwOUUnQwC5G5RcWp + 91Wnvv8 / 8A4S / 8G + v7c / 7Q / wALvAvxr + G / XV / ZCv8AwL8TfC2k + MP B + oar45 / aW8PX2o + Gdds1vNH1GfQvEX7JFhrGni9tJIbyCDULK2nMMqyPCokBP5l / tnfsb / GN / GN1 + 034f /泽+ Ol38L9Z8SeIvgzp / wAatN1 / 4UeLvGniDw8mjX / JNX / BkGkXkXi74cfDu + tdRS80C8vJ Ghtbu1Nu9qqXJmadF / Q6 / Y6 / Y7 / BQ / aFsft2of8ABQP / AIKLfB3wt8J / F3gvw1rPhD9oT4BeN / gH的rfxR0vQrXx9P4ok8ER + E / jd4GtV8H + JE8Zabo1p4xs / D519L3wTo2r31zrF8 / wDbVz + QN / BfHwLN 4Y / 4Kl / S / wDgi61 / WPFdx4T / AOCbPw80S48UeINQ1TVdd8R3WkfHT4g6bPr2tanreq63q1 / qeqzw y6he3WpapqN / JczObm + uZS1wfpeIuEsgweQZpiMHgIUa9HDc1Gp9YxrUJ + 1Svy1MTUg9NEnFq9lb c8rgrjzi7MuKsowOOzmriMLicRUhXoPC4CKqwjhq81Fyp4WFRXlCLXJOMuZRXMo6P8W9S8NSDRmn ZYmJXenLBj5rMR85Uk7mYli3yjmR2JDMc2XTJYNHsI3CKA8sCALy8ZJyhJG4u5BGGQEjBRQoJb3 + / wDDxfQktyhlERVdysACykbCQyF + CcsoK / MShYE1z134dSK0siqqdkrsGUjy0kw / zSBppti87QpZ WZiApHNfhNKhKpCV + W1Oq4yu + Ve7zRautbydnaz6arW39L + 3a5WnD3oxU4q + 7 + J73sm243fVXV3Y + fp9NSLzLMIqDBdsEv8AP87Fni24ic87hu5wA + 45Y2NM0K4htuInAuX4ZgyGMksT + 7UKwyFGMfKC doIAY16hf6VIqyM6PmVQXUeUwbD7cIoVW + 985YZIToHb5q1INKLrawIk0CBhmTcyljIGB8w53DlV GW + WASGIJbJMY6WHoWjSlLncXHk11c + ZWC + srOyTWlndt2fRheadaX7ynyQjG0uZR0cpJbtLbT VT 3a1u2eVX3h65aWKKJkRmkSNUm8yPzCVkbPmH / WFimOFU87csw2s6D4d + KdVuntNOsJJ3uJUijxFL 5e4s4Qs5jYjIxuwcNg8DC4 + ivDHhPQNzXetXskhtpQ8tiyCKSSELISRKDmAeaqlnkTc6ZIZ35P0P o3xJ8FeHLRbTQPC1nf6ze3OnR6HeXV1ZpbWsh8xJtO1bTZx9m1i3a52SRSJLAzxZguSpYyLzU8BW xPLUnP2NNJNwabnJ8ztZQb0te930ersicTmSo88aFF4qaso + 8qUOt3zyWltOjvq03sfFGl / s2fFP V77TrLTtLsL + / uREsdraz3DJBLc3ItLVb + 6aBbTTlnn2wRz3dwlmk8qJPKu8McvW / hN8V / BfiTWP A3iXwF4m0fxNo2rXOgXuiyWdzLex6taXstneWSRQ2xWdhcJ5QaMurK8b26yIQ9fZ0nxi8WeBr7x3 cTeINS8GTTW82mXU + m65d6LpI8OXNzNc3ehXsiXNvHfaQouP9AgvLq4tYYJmjiBj2MfK9G + KWif2 zbazpuoXVxLOWuLTVluLre4Aje0vLdi8EsyuI45ra5 + Uy5 + 0QSmMI7c + cU8FTw14wx8ZRlBVK6jG pSS52lGFP2Mp88ttJX967binfXJsXmUq1SdSpl0KdSMIQpSnU9spc825VKiqezUEtrKab5tYu7Pm fTNMuoZ9kuhalDJbmMzGazk / dvLMzEFREreYAmZFYOqqEXerbUPYy + H4mMIn0u6TzkyHu7Se337d 6mUfaLf5o3bIzhhvGGyVzX1rpHjHwTqP2ybxP4tNpqDzxu9uNB169llBK42S6Zpt / wCYxLyyPLLK ZvlCAMzBW9Bj + J / wYLQWl541EIiCW8l3f + GvG1zLGigqMGXw5Kmx2ySkbny + VKoQ5r8uzzM6lGT + qYDNcTGKjCMv7PquU2rty5Yq9ktbpJWtdNq5 + oZRg8JWajisflNNzhzSl / aNKKjLmcbS9pKCS Tu9 XdJq + rkj4o0nwxGEuMRxGOJWRDsBI3wjesWIFUfIVRcYB2

但是服务器没有上传它。请帮我解决这个问题。

android python bitmap
1个回答
0
投票

试试这个,对于带有Flask api的服务器端来获取图像(base64)并将其保存到服务器中。

@app.route('/api/create_category',methods=['POST'])
def image():
user_id = request.json["user_id"]
cat_name = request.json["category_name"] 
desc = request.json["description"]
img = request.json["image"]
filename = (user_id+date+".png")
img_url = "/".join([target,filename])
with open(img_url, "wb") as fh:
    fh.write(base64.b64decode(img))
print(img_url,file=sys.stderr)
db.category.insert({"user_id":user_id,"category_name":cat_name,"desription":desc,"img_url":img_url})
return jsonify({"result":{"message":"success"}})

Android表单需要使用Volley在base64上传图片

    HashMap<String,String> params =  new HashMap<>();
            params.put("user_id",user_id);
            params.put("category_name",category_name);
            params.put("description",description);
            params.put("image", imagetoString(bitmap));
            JSONObject parameter =  new JSONObject(params);
    JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.POST, URL, parameter, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.d("response", response.toString());
            try {
                String result = response.getJSONObject("result").get("message").toString();

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });


RequestQueue queue = Volley.newRequestQueue(this);
    queue.add(jsonObject);
© www.soinside.com 2019 - 2024. All rights reserved.