在Android中发布图像和Volley Library的一些字符串

时间:2017-10-19 08:36:39

标签: android json image post android-volley

我是Android的新手,我知道如何使用凌空发布字符串,但现在我想发送带有图像的字符串! 现在我从画廊中选择了一张图片,但我无法发布它

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == request && resultCode == RESULT_OK && data!= null){
        Uri uri = data.getData();
        try {
            bitmap= MediaStore.Images.Media.getBitmap(getContentResolver(),uri);
            img.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

void selectimg(){
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(intent,request);
}

1 个答案:

答案 0 :(得分:0)

This is how you convert your image to a string

要从转换后的图像中发布字符串,请尝试使用:

    public void postStringofImage() {
    String URL = "URL OF YOUR WEBSITE";
    RequestQueue queue = Volley.newRequestQueue(/* Your Context*/);
    StringRequest putRequest = new StringRequest(Request.Method.POST, URL,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response) {
                    // Handle response
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // Handle error
                }
            }
    ) {

        @Override
        protected Map<String, String> getParams() {
            Map<String, String>  params = new HashMap<String, String>();
            params.put("KEY OF YOUR IMAGE", "VALUE OF YOUR IMAGE FROM THE BASE64 ENCODING");
            return params;
        }


    };
    queue.add(putRequest);
}

祝你好运!