1

I have an android application that needs to receive several pictures from the webservice. But how to do this?

In my webservice i'm currently sending only 1 image as a byte[].

public static byte[] GetMapPicture(string SeqIndex)
    {
        try
        {
            byte[] maps;
            InterventionEntity interventie = new InterventionEntity(long.Parse(SeqIndex));
            MyDocumentsCollection files = interventie.Location.MyDocuments;
            maps = null;
            foreach (MyDocumentsEntity file in files)
            {
                if (file.SeqDocumentType == (int)LocationDocumentType.GroundPlanDocument && file.File.Filename.EndsWith(".jpg"))
                    maps = (file.File.File);
            }
            return maps;
        } catch (Exception e) {
            Log.Error(String.Format("Map not send, {0}", e));
            return null;
        }
    }

The byte[] is returned from my webservice. But in my android project the bitmap is not decoded and therefor null.

public Bitmap getPicture(String message, String url, Context context) throws IOException{
     HttpClient hc = MySSLSocketFactory.getNewHttpClient();
     Log.d(MobileConnectorApplication.APPLICATION_TAG, "NETWORK - Message to send: "+ message);
     HttpPost p = new HttpPost(url);
     Bitmap picture;
    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setSoTimeout(httpParams, threeMinutes );
    p.setParams(httpParams);

     try{
        if (message != null)
            p.setEntity(new StringEntity(message, "UTF8"));
     }catch(Exception e){
         e.printStackTrace();
     }
     p.setHeader("Content-type", "application/json");

     HttpContext httpcontext = new BasicHttpContext();
    httpcontext.setAttribute(ClientContext.COOKIE_STORE, MobileConnectorApplication.COOKIE_STORE);
    try{
         HttpResponse resp = hc.execute(p,httpcontext);
         InputStream is = resp.getEntity().getContent();

         picture = BitmapFactory.decodeStream(is);  //here is goes wrong
         int httpResponsecode = resp.getStatusLine().getStatusCode() ;
         checkResponse(url, message, "s", httpResponsecode);
         Log.d(MobileConnectorApplication.APPLICATION_TAG, String.format("NETWORK - Response %s", httpResponsecode));

    } finally{

    }
     return picture;
 }

Can anyone help me on this?

Robin
  • 89
  • 1
  • 2
  • 9

1 Answers1

1

assuming incomingbytearray is a byte array,

Bitmap bitmapimage = BitmapFactory.decodeByteArray(incomingbytearray, 0, incomingbytearray.length);
String filepath = "/sdcard/xyz.png";
File imagefile = new File(filepath);
FileOutputStream fos = new FileOutputStream(imagefile);
bitmapimage.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();

This should be fine.

EDIT: input stream to bytearray,

InputStream in = new BufferedInputStream(url.openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();

conversion code from Android: BitmapFactory.decodeByteArray gives pixelated bitmap

Community
  • 1
  • 1
Vamsi
  • 5,853
  • 6
  • 29
  • 36
  • and how do i get the "incomingbytearray" from the httpresponse? – Robin Jan 09 '12 at 11:16
  • since you are using stream, instead of bytearray, use Inputstream; Bitmap bitmapimage = BitmapFactory::decodeStream(InputStream is) – Vamsi Jan 09 '12 at 11:30
  • Like i said in my question, I already do that, and even if i converted the inputstream to bytearray, i still get null. – Robin Jan 09 '12 at 11:36
  • can you try the below stmt, ByteArrayInputStream mByteArrayInputStream = new ByteArrayInputStream(is); – Vamsi Jan 09 '12 at 12:14
  • ByteArrayInputStream only accepts byte[] as input, not an inputStream So you want me to read the bytes from the inputstream, to make a byte[], and then put it into another stream? that makes no sense to me ... – Robin Jan 09 '12 at 12:22
  • check, BitMapFactory::decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts) with bmOptions = new BitmapFactory.Options(); bmOptions.inSampleSize = 1; – Vamsi Jan 12 '12 at 11:03