Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 14
Registered: ‎11-22-2008
My Device: 9360
My Carrier: T-Mobile CZ
Accepted Solution

KSOAP2 and byte array

Hi,
I have a request. I have a MSSQL database in two column. One is a varchar and the second is the type of Image. I would like to use these two fields KSOAP2 filled. I tried to send a picture as byte array KSOAP but did not take it, probably I does sometimes mistake.
Here is the source code.

 

public byte[] getBytesFromBitmap(Bitmap bmp) { 
 try {
  int height=bmp.getHeight();
  int width=bmp.getWidth();
  int[] rgbdata = new int[width*height];
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  DataOutputStream dos = new DataOutputStream(bos);
  bmp.getARGB(rgbdata,0,width,0,0,width,height);
  for (int i = 0; i < rgbdata.length ; i++) {
   if (rgbdata[i] != -1) {
    dos.writeInt(i);
    dos.flush();
    //l++; 
   }
  } 
  bos.flush();
  return bos.toByteArray(); 
 } catch (Exception ex) {
  Dialog.alert("getBytesFromBitmap: " + ex.toString()); return null; 
 } 
}




Bitmap borderBitmap = Bitmap.getBitmapResource("rounded.png");
byte[] img = getBytesFromBitmap(borderBitmap);
String name = "Name";
SoapObject rpc = new SoapObject(serviceNamespace, "name");

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
rpc.addProperty("_img", img);
rpc.addProperty("_string", name);
envelope.setOutputSoapObject(rpc);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.encodingStyle = SoapSerializationEnvelope.XSD;

 

I also tried this on ... I transferred the image to byte array and then into a string, using string I sent KSOAP and everything went well ... The problem was that if it string convert back to byte array and then into a Bitmap, so bb don't show me Bitmap. I would like to send a byte array.

 

 

Thank you

 

Stepan

Developer
Posts: 209
Registered: ‎04-30-2011
My Device: BlackBerry Z10
My Carrier: Airtel

Re: KSOAP2 and byte array

[ Edited ]

This function is useful for get the byte from image.

public static byte[] returnByte(String path)
{
FileConnection fconn;
int available = 0;
InputStream input = null;
byte[] data=null;
try
{
fconn = (FileConnection)Connector.open(path);
if(fconn.exists()==true)
{
input = fconn.openInputStream();
available = input.available();
int fSz = (int)fconn.fileSize();
data = new byte[fSz];
input.read(data, 0, fSz);
}
}
catch(Exception e)
{
Dialog.alert("Error");
}
return data;
}

 
Now you need to do this:


byte[] img = returnByte("pawan.png");

rpc.addProperty("_img", Base64.encode(img));

I am sure this will work.. 

Thanks 

 

Contributor
Posts: 14
Registered: ‎11-22-2008
My Device: 9360
My Carrier: T-Mobile CZ

Re: KSOAP2 and byte array

hi,

thank you... but can i use this function if my image is in resources... image not save on sd card but is save in resource in java app... 

Developer
Posts: 209
Registered: ‎04-30-2011
My Device: BlackBerry Z10
My Carrier: Airtel

Re: KSOAP2 and byte array

Yes, you can do any image.

Let me know if you are not able to send still.
Highlighted
Contributor
Posts: 14
Registered: ‎11-22-2008
My Device: 9360
My Carrier: T-Mobile CZ

Re: KSOAP2 and byte array

Hi,

thank you so much... working perfect Smiley Happy

 

Stepan