2012-01-09 1 views
1

웹 서비스에서 여러 장의 사진을 받아야하는 안드로이드 응용 프로그램이 있습니다. 하지만 어떻게해야합니까?안드로이드가 webservice에서 사진을 받고, 어떻게?

내 webservice에서 현재 byte []로 1 이미지 만 보내고 있습니다.

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; 
     } 
    } 

바이트 []가 내 웹 서비스에서 반환됩니다. 하지만 안드로이드 프로젝트에서 비트 맵은 디코딩되지 않으므로 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; 
} 

아무도 도와 줄 수 있습니까?

답변

1

가정 incomingbytearray는

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(); 

이것은 잘해야, 바이트 배열이다.

편집 : 입력 스트림 Android: BitmapFactory.decodeByteArray gives pixelated bitmap

+0

에서,

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(); 

변환 코드를 BYTEARRAY하고 내가 HttpResponse에에서 "incomingbytearray"를 어떻게받을 수 있나요? – Robin

+0

bytearray 대신 stream을 사용하고 있으므로 Inputstream을 사용하십시오. 비트 맵 bitmapimage = BitmapFactory :: decodeStream (InputStream is) – Vamsi

+0

내가 말했듯이, 내 질문에, 나는 이미 그것을하고, 심지어 입력 스트림을 bytearray로 변환해도 여전히 null이됩니다. – Robin

관련 문제