0

I have had a issue with image uploading. I have an application that when the user clicks "Upload Image" they are displayed with an alert box that says use camera or use photo gallery.

once the user chooses one they are either triggered to the camera intent or gallery intent and that works fine. Getting the image works fine as well that is onActivityResult.

HOWEVER once the image has been taken/chosen I want to upload it to my server this is were the PROBLEM occurs. Only the CAMERA image works to add to my BasicNameValuePair how can I code so that if the camera option has been chosen then add that image or if the photo gallery has been chosen upload that image to the server????

Thread loadingThread = new Thread(){

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try{

                if (cameraImage.getByteCount()!=0)
                {   
                    ByteArrayOutputStream BArrayOutPut = new ByteArrayOutputStream();
                    cameraImage.compress(Bitmap.CompressFormat.JPEG, 100, BArrayOutPut);
                    byte [] byteArray = BArrayOutPut.toByteArray();
                    String capturedImageInString = Base64.encodeBytes(byteArray);

                    Log.i("Camera_Image","Image " +capturedImageInString);
                    try {
                    nvp.add(new BasicNameValuePair("Camera_Image", capturedImageInString)); 
                    }catch (Exception e){Log.i("nvpADD","Error is " +e.toString());}
                }else if (uploadImage.getByteCount()!=0){
                    try{
                        ByteArrayOutputStream BArrayOutPut = new ByteArrayOutputStream();
                        uploadImage.compress(Bitmap.CompressFormat.JPEG, 100, BArrayOutPut);
                        byte [] byteArray = BArrayOutPut.toByteArray();
                        String uploadImageInString = Base64.encodeBytes(byteArray);

                        Log.i("log_Upload","UploadedImage " +uploadImageInString);

                        nvp.add(new BasicNameValuePair("Camera_Image", uploadImageInString));

                    }catch (Exception e){
                        Log.e ("LOG_IMAGEUPLOAD", "Error uploading Image "+e.toString());e.printStackTrace();
                        }

                }


                // Get input values of EditTexts
                String UserName = username.getText().toString().trim();
                String ProductTitle = productTitle.getText().toString().trim();
                String ProductPrice = productPrice.getText().toString().trim();
                String ProductDescription = productDescription.getText().toString().trim();
                String ProductCategory = productCategory.getSelectedItem().toString().trim();


                //Adding string values to the array
                nvp.add(new BasicNameValuePair("TextView_Username", UserName));                             
                nvp.add(new BasicNameValuePair("EditText_Title", ProductTitle));
                nvp.add(new BasicNameValuePair("EditText_Price", ProductPrice));
                nvp.add(new BasicNameValuePair("EditText_Description", ProductDescription));
                nvp.add(new BasicNameValuePair("Spinner_Category", ProductCategory));

                httpPost.setEntity(new UrlEncodedFormEntity(nvp));


            ResponseHandler<String> RegisterHandler = new BasicResponseHandler();
            final String respond = httpClient.execute(httpPost,RegisterHandler);


            runOnUiThread(new Runnable() {
Almanzt
  • 147
  • 1
  • 1
  • 8

0 Answers0