使用Volley android上传图像的问题?

时间:2018-02-10 06:46:29

标签: java android http post android-volley

我正在尝试使用android的凌空网络库将图像上传到我的ImgUr帐户。但是,出于某种原因,没有任何事情发生。我试过检查日志但由于某种原因,我甚至没有收到任何错误。我能做错什么?

/**
 */

public class UploadService extends Activity{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.push_activity);
        encodeImage();

        Button b1 =(Button)findViewById(R.id.pushfiles);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RequestQueue queue = Volley.newRequestQueue(UploadService.this);
                String url = "https://api.imgur.com/3/image";
                StringRequest uploadRequest = new StringRequest(com.android.volley.Request.Method.POST, url, new com.android.volley.Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, "finished image upload");
                    }
                }, new com.android.volley.Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e(TAG, error.toString());
                        Log.e(TAG, "finish/error upload");
                    }
                }) {
                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        Map<String, String> headers = new HashMap<>();
                        headers.put("Authorization", "Client-ID " + "**********");
                        return headers;
                    }

                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> params = new HashMap<>();
                        params.put("image", encodeImage());

                        return params;
                    }

                };

                queue.add(uploadRequest);

            }
        });
    }

    public String encodeImage(){
        String imgpath = Environment.getExternalStorageDirectory().getPath() + "/Android/data/c.png";
        //Bitmap bm = BitmapFactory.decodeFile(R.drawable.hand);
        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.hand);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object
        byte[] b = baos.toByteArray();
        String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
        return encodedImage;
    }

}

0 个答案:

没有答案