0

In my app I need an image chooser option, by which user can choose image from library, also take image from camera to upload it to server. Now , I use volley library to sent Post request from the app. The image should be in jpg format as server only supports jpeg format and not more that 10,000 byte. In my code what I have done so far, take the image or choose image from gallery. Make a method to resize the image, but that is now actually working. And then try to upload the image as soon as User will select image from gallery. at this moment the image is hit the server, also remove the image from server, but not actually show the image, because of compressing error. I stuck with this issue, but could not solve the problem. How can I resize the jpeg image into 10,000 byte and send it as parameter in volley?

Here is my code for photo uploading funtionality

private void galleryIntent()
{
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType("image/*");
    startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_FILE);

}

private void cameraIntent()
{
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_CAMERA);
    }
}

private void uploadImage()
{
    StringRequest stringRequest = new StringRequest( Request.Method.POST, UPLOAD_URL+mail,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                    //Showing toast message of the response
                Toast.makeText(getActivity(), response , Toast.LENGTH_LONG).show();
                Log.d( "Error Response"," "+response );
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                    //Showing toast
                Toast.makeText(getActivity(), volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
                Log.d( "Volley Error"," "+volleyError );
            }
        }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {

                //Problem in this line as the image is on jpeg format in server side.

              //Creating parameters
                    //Here I want to set resize image as parameter 
            Map<String,String> params = new Hashtable<String, String>();
            String imageData=imageToString( bitmap );
            params.put("image", imageData);

            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {

            Map<String, String> headers = new HashMap<>();
            headers.put( "Content-Type", "image/jpeg" );
            Log.d( "headers", String.valueOf( headers ) );
            return headers;
        }
    };
        //Creating a Request Queue
    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
        //Adding request to the queue
    requestQueue.add(stringRequest);
}


// Here I want to resize the jpeg image into 10 byte.
// I have an idea but do not know actually how can I implement that.
 public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
    int width = 200;
    int height = 200;
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);

    // "RECREATE" THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(
            bm, 0, 0, width, height, matrix, false);
    bm.recycle();
    return resizedBitmap;
}


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

    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_FILE){
            Uri filepath=data.getData();
            try {
                InputStream inputStream=getActivity().getContentResolver().openInputStream( filepath );
                bitmap= BitmapFactory.decodeStream( inputStream );
                image.setImageBitmap( bitmap );

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    else if (requestCode == REQUEST_CAMERA) {

        if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) {
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            thumbnail.compress(Bitmap.CompressFormat.JPEG,10, bytes);
            File destination = new File(Environment.getExternalStorageDirectory(),"temp.jpg");
            FileOutputStream fo;
            try {
                fo = new FileOutputStream(destination);
                fo.write(bytes.toByteArray());
                fo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            image.setImageBitmap(thumbnail);
        }
    }

    uploadImage();
}
tamrezh21
  • 105
  • 2
  • 13

0 Answers0