Post Reply

Unable to upload picture when sending as Base64 String

OP Mamatha

31st January 2014, 05:50 AM   |  #1  
OP Junior Member
Thanks Meter: 0
 
2 posts
Join Date:Joined: Jan 2014
I am trying to send an image to our asp.net webservice from android.Here is my sample code :

// Getting image from Gallery
Code:
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(selectedImage,
					filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);

cursor.close();

/*	BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;*/

thumbnail = (BitmapFactory.decodeFile(picturePath));
img_photo.setImageBitmap(thumbnail);
// converting imag into base64 string

Code:
img_photo.buildDrawingCache();
		Bitmap bm = img_photo.getDrawingCache();
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.PNG, 100, baos); // bm is the bitmap
															
		byte[] photo = baos.toByteArray();
		System.out.println("this is byte array" + bytearray);
		
		 String temp_base =Base64.encodeToString(photo,Base64.NO_WRAP);
// calling webservice

Code:
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

request.addProperty("CarID", SellCarDetailView.sellcardetails_carid);
request.addProperty("pic",temp_base);
		System.out.println("this is piccontent" +temp_base);
try {

			SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
soapEnvelope.encodingStyle = SoapEnvelope.ENC;
			// new MarshalBase64().register(soapEnvelope);
			soapEnvelope.dotNet = true;
			soapEnvelope.setOutputSoapObject(request);
			 HttpTransportSE aht = new HttpTransportSE(URL);
			//AndroidHttpTransport aht = new AndroidHttpTransport(URL);
			aht.call(SOAP_ACTION, soapEnvelope);

			// SoapObject response = (SoapObject)envelope.getResponse();
			SoapPrimitive response = (SoapPrimitive) soapEnvelope.getResponse();
			String temp3 = response.toString();
		
			Log.v("TAG", temp3);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
How ever i am getting "invalid parameter" at web service end.

// Asp.net code


Code:
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Xml)]
[WebMethod(EnableSession = true)]
public string UploadPictureByCarIDFromAndroid(string CarID, string make, string model, string year, string UserID, string pic, string AuthenticationID, string CustomerID, string SessionID)
{
    
    string bStatus = "Failed";
    MobileBL objMobile = new MobileBL();
    UsedCarsInfo objCarPicInfo = new UsedCarsInfo();
    
    try
    {
                  try
            {
                if (AuthenticationID == ConfigurationManager.AppSettings["AppleID"].ToString())
                {
                    objCarPicInfo.Carid = Convert.ToInt32(CarID);
                    byte[] picContent = Convert.FromBase64String(pic);
                    // byte[] picContent = Base64.decode(pic);
                    MemoryStream ms = new MemoryStream(picContent, 0,picContent.Length); // getting "invalid length"
                    ms.Write(picContent, 0, picContent.Length);
                    Bitmap oBitmap1 = new Bitmap(ms);// getting "invalid length" error here
                    // System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
                    
                    
                    
                    
                }
            }
            catch (Exception ex)
            {
                
            }
        
    }
    catch (Exception ex)
    {
    }
    
    
    return bStatus;
}
I am getting "invalid length" error when sending the image.Any help is highly appreciated.
31st January 2014, 12:08 PM   |  #2  
warlock9_0's Avatar
Member
Flag Athens
Thanks Meter: 9
 
32 posts
Join Date:Joined: Apr 2013
More
i think a better approach is with stream rather file path
for example you can try

Get image from gallery
Code:
Uri selectedImage = data.getData();
BitmapFactory.Options options = new BitmapFactory.Options();
//your options

Bitmap img_photo = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, options);
convert to base64
Code:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
img_photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
String temp_base =Base64.encodeToString(byteArray,Base64.DEFAULT);

i cant see anything wrong in the web services call but shouldn't you send all the parameters the function needs?

also, try to change this
Code:
// SoapObject response = (SoapObject)envelope.getResponse();
SoapPrimitive response = (SoapPrimitive) soapEnvelope.getResponse();
String temp3 = response.toString();
to this
Code:
SoapObject response = (SoapObject)envelope.getResponse();
String temp3 = response.getProperty(0).toString();
Last edited by warlock9_0; 31st January 2014 at 12:37 PM.
3rd February 2014, 11:42 AM   |  #3  
OP Junior Member
Thanks Meter: 0
 
2 posts
Join Date:Joined: Jan 2014
Unable to upload picture when sending as Base64 String
Quote:
Originally Posted by warlock9_0

i think a better approach is with stream rather file path
for example you can try

Get image from gallery

Code:
Uri selectedImage = data.getData();
BitmapFactory.Options options = new BitmapFactory.Options();
//your options

Bitmap img_photo = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, options);
convert to base64
Code:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
img_photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
String temp_base =Base64.encodeToString(byteArray,Base64.DEFAULT);

i cant see anything wrong in the web services call but shouldn't you send all the parameters the function needs?

also, try to change this
Code:
// SoapObject response = (SoapObject)envelope.getResponse();
SoapPrimitive response = (SoapPrimitive) soapEnvelope.getResponse();
String temp3 = response.toString();
to this
Code:
SoapObject response = (SoapObject)envelope.getResponse();
String temp3 = response.getProperty(0).toString();

Thanks for giving reply, i tried your code but getting the same problem.
3rd February 2014, 03:21 PM   |  #4  
warlock9_0's Avatar
Member
Flag Athens
Thanks Meter: 9
 
32 posts
Join Date:Joined: Apr 2013
More
sending to web service is ok?

trying this in the web service end?
Code:
Bitmap bmpReturn = null; 
 
    byte[] byteBuffer = Convert.FromBase64String(base64String); 
    MemoryStream memoryStream = new MemoryStream(byteBuffer); 
 
    memoryStream.Position = 0; 
 
    bmpReturn = (Bitmap)Bitmap.FromStream(memoryStream); 
 
    memoryStream.Close(); 
    memoryStream = null; 
    byteBuffer = null;
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Top Threads in Java for Android App Development by ThreadRank