使用Volley上传图像(超过2 MB)时出错

时间:2017-02-07 07:29:27

标签: android android-volley image-uploading socketexception

我在上传超过2 MB的图片时遇到问题。 请帮我解决这个问题。这个错误

  

java.net.SocketException:Broken pipe

Toast 显示。

enter image description here

这是以Base64格式获取图像并在服务器上上传的代码。

public String getStringImage(Bitmap bmp){
    ByteArrayOutputStream baos=new  ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG,100, baos);
    byte [] b=baos.toByteArray();
    String encodedImage= null;
    try{
        System.gc();
        encodedImage= Base64.encodeToString(b, Base64.DEFAULT);
    }catch(Exception e){
        e.printStackTrace();
    }catch(OutOfMemoryError e){
        baos=new  ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG,50, baos);
        b=baos.toByteArray();
        encodedImage=Base64.encodeToString(b, Base64.DEFAULT);
        Log.e("EWN", "Out of memory error catched");
    }
    return encodedImage;
}

private void uploadImage(){
    //Showing the progress dialog
    final ProgressDialog loading = ProgressDialog.show(getActivity(), "Uploading...", "Please wait...", false, false);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.UPLOADIMAGE_URL+"/user",
        new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    //Disimissing the progress dialog
                    loading.dismiss();
                    //Showing toast message of the response
                    System.out.println("Response :" + s);

                    JSONObject jsonObject = new JSONObject(s);
                    Preferences.setUserImageUrl(getActivity(), jsonObject.getString("image_name"));

                    //  MainActivity mains = new MainActivity();
                    //  Picasso.with(mains).load(finalImageUrl).placeholder(R.drawable.profile_image).into(mains.profile_image);
                    //  mains.updateimage(jsonObject.getString("image_name"));
                } catch (JSONException e) {
                    e.printStackTrace();

                }

                // Toast.makeText(getActivity(), s , Toast.LENGTH_LONG).show();
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                //Dismissing the progress dialog
                loading.dismiss();
               if (volleyError.getMessage()==null){

                   Toast.makeText(getActivity(), "Image not uploaded Please check your internet ", Toast.LENGTH_SHORT).show();

               }else {
                //Showing toast
                Toast.makeText(getActivity(), volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
            }
            }
        }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            //Converting Bitmap to String
            String image = getStringImage(bitmap);

            //Creating parameters
            Map<String,String> params = new Hashtable<String, String>();

            //Adding parameters
            params.put("image", image);
            System.out.println("image : " + image);
            params.put("id", Preferences.getUserId(getActivity()));
            System.out.println("ID : "+Preferences.getUserId(getActivity()));

            return checkParams(params);
        }
        //returning parameters
    };
    //Creating a Request Queue
    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());

    //Adding request to the queue
    requestQueue.add(stringRequest);

    //AppController.getInstance().addToRequestQueue(stringRequest);
}

private Map<String, String> checkParams(Map<String, String> map) {
    Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, String> pairs = (Map.Entry<String, String>) it.next();
        if (pairs.getValue() == null) {
            map.put(pairs.getKey(), "image");
            map.put(pairs.getKey(), "id");
        }
    }
    return map;
}

private void showFileChooser() {
    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);


    if (requestCode == PICK_IMAGE_REQUEST && resultCode == getActivity().RESULT_OK && data != null && data.getData() != null) {
        Uri filePath = data.getData();
        try {
            //Getting the Bitmap from Gallery
            bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
            //Setting the Bitmap to ImageView

            userImage.setImageBitmap(bitmap);
            uploadImage();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这是我的代码..

0 个答案:

没有答案