Android получает фотографии с веб-сервиса, как? - PullRequest
1 голос
/ 09 января 2012

У меня есть приложение для Android, которому нужно получить несколько картинок из веб-сервиса. Но как это сделать?

В моем веб-сервисе я сейчас отправляю только 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;
        }
    }

Байт [] возвращается из моего веб-сервиса. Но в моем проекте Android растровое изображение не декодируется и поэтому ноль.

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 Ответ

1 голос
/ 09 января 2012

при условии, что входящий байтовый массив является байтовым массивом,

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

Все должно быть в порядке.

РЕДАКТИРОВАТЬ: поток ввода в байтовый массив,

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

код преобразования из Android: BitmapFactory.decodeByteArray дает пиксельное растровое изображение

...