How to upload Image on server using Volley?

后端 未结 2 646
自闭症患者
自闭症患者 2020-12-30 14:44

I\'m trying to post my data using Volley but my i\'m not able to upload my image on server. Always getting error like unexpected response code 500 for

2条回答
  •  灰色年华
    2020-12-30 15:20

    You can try this method ,which actually worked for ma project.First you have to pick the image from gallery then it must be converted to string and sent via volley to server

    // initialize
       private int PICK_IMAGE_REQUEST = 1;
    
        //set click listener 
             Upload.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //method to upload the image
                             showFileChooser();
           
                        }
                    });
            
    

    Method to open the gallery and to pick the image

                     private void showFileChooser() {
                         Intent pickImageIntent = new Intent(Intent.ACTION_PICK,
                     android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                                    pickImageIntent.setType("image/*");
                                    pickImageIntent.putExtra("aspectX", 1);
                                    pickImageIntent.putExtra("aspectY", 1);
                                    pickImageIntent.putExtra("scale", true);
                                    pickImageIntent.putExtra("outputFormat",
                                    Bitmap.CompressFormat.JPEG.toString());
                                    startActivityForResult(pickImageIntent, PICK_IMAGE_REQUEST); 
                             }
                  
    

    Add this method.Here the image is actually getting sent to server.

      @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                            super.onActivityResult(requestCode, resultCode, data);
                            if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
                                Uri filePath = data.getData();
                                try {
                                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                                    Bitmap lastBitmap = null;
                                    lastBitmap = bitmap;
                                   //encoding image to string  
                                    String image = getStringImage(lastBitmap);
                                    Log.d("image",image);
                                    //passing the image to volley
                                    SendImage(image);
                             
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
            
           
    
     
    

    Method to encode the image as string

     public String getStringImage(Bitmap bmp) {
                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                            bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                            byte[] imageBytes = baos.toByteArray();
                            String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
                            return encodedImage;
                    
                        }
                
        
    
    
    
        
    

    uploading using volley

     private void SendImage( final String image) {
                final StringRequest stringRequest = new StringRequest(Request.Method.POST, "URL",
                        new Response.Listener() {
                            @Override
                            public void onResponse(String response) {
                                Log.d("uploade",response);
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                   
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
        
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Toast.makeText(Edit_Profile.this, "No internet connection", Toast.LENGTH_LONG).show();
                               
                            }
                        }) {
                    @Override
                    protected Map getParams() throws AuthFailureError {
        
                        Map params = new Hashtable();
                      
                        params.put("image", image);
                        return params;
                    }
                };
                {
                    int socketTimeout = 30000;
                    RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
                    stringRequest.setRetryPolicy(policy);
                    RequestQueue requestQueue = Volley.newRequestQueue(this);
                    requestQueue.add(stringRequest);
                }
            }
    

提交回复
热议问题