将图像从Android发送到服务器

时间:2020-04-03 09:03:41

标签: android arrays image rest http

我尝试通过Java将图像从Android发送到服务器,但是在我的数据库中我得到了这样的信息: [B @ 2f2d4a9 我使用下一个代码:

 Bitmap bitmap = ((BitmapDrawable) userAva.getDrawable()).getBitmap();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] imageInByte = baos.toByteArray();


        URL url = new URL ("https://**********.net/People/CreatePerson");
        HttpURLConnection con = (HttpURLConnection)url.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json; utf-8");
        con.setRequestProperty("Accept", "application/json");
        con.setDoOutput(true);
        String jsonInputString = "{\"nickname\": \"Upendra\", \"photo\":" +imageInByte+"}";
        //String jsonInputString = "{\"nickname\": \"Upendra\"}";

        try(OutputStream os = con.getOutputStream()) {
            byte[] input = jsonInputString.getBytes("utf-8");
            os.write(input, 0, input.length);
        }

        try(BufferedReader br = new BufferedReader(
                new InputStreamReader(con.getInputStream(), "utf-8")))

0 个答案:

没有答案