0

我在上传图片时遇到了问题。我有一个应用程序,当用户单击“上传图像”时,它们会显示一个警告框,上面写着使用相机或使用照片库。

一旦用户选择了一个,它们就会被触发到相机意图或画廊意图,并且效果很好。获取图像也可以正常工作,即 onActivityResult。

但是,一旦拍摄/选择了图像,我想将其上传到我的服务器,这就是问题的发生。只有 CAMERA 图像可以添加到我的 BasicNameValuePair 我如何编码以便如果选择了相机选项然后添加该图像或者如果选择了照片库将该图像上传到服务器????

线程加载线程 = 新线程(){

        @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() {
4

0 回答 0