Unable to upload picture when sending as Base64 String

  • Replies:0
Mamatha Kota
  • Forum posts: 1

Jan 31, 2014, 6:31:09 AM via Website

I am trying to send an image to our asp.net web service from android.Here is my sample code :

// Getting image from Gallery

1Uri selectedImage = data.getData();
2String[] filePathColumn = { MediaStore.Images.Media.DATA };
3
4Cursor cursor = getContentResolver().query(selectedImage,
5 filePathColumn, null, null, null);
6cursor.moveToFirst();
7
8int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
9picturePath = cursor.getString(columnIndex);
10
11cursor.close();
12
13/* BitmapFactory.Options options = new BitmapFactory.Options();
14options.inSampleSize = 4;*/
15
16thumbnail = (BitmapFactory.decodeFile(picturePath));
17img_photo.setImageBitmap(thumbnail);

// converting imag into base64 string

1img_photo.buildDrawingCache();
2 Bitmap bm = img_photo.getDrawingCache();
3
4 ByteArrayOutputStream baos = new ByteArrayOutputStream();
5 bm.compress(Bitmap.CompressFormat.PNG, 100, baos); // bm is the bitmap
6
7 byte[] photo = baos.toByteArray();
8 System.out.println("this is byte array" + bytearray);
9
10 String temp_base =Base64.encodeToString(photo,Base64.NO_WRAP);

// calling webservice

1SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
2
3request.addProperty("CarID", SellCarDetailView.sellcardetails_carid);
4request.addProperty("pic",temp_base);
5 System.out.println("this is piccontent" +temp_base);
6try {
7
8 SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
9 SoapEnvelope.VER11);
10soapEnvelope.encodingStyle = SoapEnvelope.ENC;
11 // new MarshalBase64().register(soapEnvelope);
12 soapEnvelope.dotNet = true;
13 soapEnvelope.setOutputSoapObject(request);
14 HttpTransportSE aht = new HttpTransportSE(URL);
15 //AndroidHttpTransport aht = new AndroidHttpTransport(URL);
16 aht.call(SOAP_ACTION, soapEnvelope);
17
18 // SoapObject response = (SoapObject)envelope.getResponse();
19 SoapPrimitive response = (SoapPrimitive) soapEnvelope.getResponse();
20 String temp3 = response.toString();
21
22 Log.v("TAG", temp3);
23
24 } catch (Exception e) {
25 e.printStackTrace();
26 }

How ever i am getting "invalid parameter" at web service end.

// Asp.net code
1[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Xml)]
2[WebMethod(EnableSession = true)]
3public string UploadPictureByCarIDFromAndroid(string CarID, string make, string model, string year, string UserID, string pic, string AuthenticationID, string CustomerID, string SessionID)
4{
5
6 string bStatus = "Failed";
7 MobileBL objMobile = new MobileBL();
8 UsedCarsInfo objCarPicInfo = new UsedCarsInfo();
9
10 try
11 {
12 try
13 {
14 if (AuthenticationID == ConfigurationManager.AppSettings["AppleID"].ToString())
15 {
16 objCarPicInfo.Carid = Convert.ToInt32(CarID);
17 byte[] picContent = Convert.FromBase64String(pic);
18 // byte[] picContent = Base64.decode(pic);
19 MemoryStream ms = new MemoryStream(picContent, 0,picContent.Length); // getting "invalid length"
20 ms.Write(picContent, 0, picContent.Length);
21 Bitmap oBitmap1 = new Bitmap(ms);// getting "invalid length" error here
22 // System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
23
24
25
26
27 }
28 }
29 catch (Exception ex)
30 {
31
32 }
33
34 }
35 catch (Exception ex)
36 {
37 }
38
39
40 return bStatus;
41}

I am getting "invalid length" error when sending the image.Any help is highly appreciated.

Reply