【问题标题】:Android getting pictures from webservice, how to?Android从webservice获取图片,如何?
【发布时间】:2012-01-09 10:21:53
【问题描述】:

我有一个需要从 web 服务接收多张图片的 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;
        }
    }

byte[] 是从我的网络服务返回的。 但在我的 android 项目中,位图未解码,因此为 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;
 }

谁能帮我解决这个问题?

【问题讨论】:

    标签: android bitmap bytearray


    【解决方案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();
    

    这应该没问题。

    编辑:字节数组的输入流,

    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 gives pixelated bitmap的转换码

    【讨论】:

    • 我如何从 httpresponse 获取“incomingbytearray”?
    • 因为你使用的是stream,而不是bytearray,所以使用Inputstream;位图bitmapimage = BitmapFactory::decodeStream(InputStream is)
    • 就像我在问题中所说的那样,我已经这样做了,即使我将输入流转换为字节数组,我仍然会得到 null。
    • 你可以试试下面的stmt,ByteArrayInputStream mByteArrayInputStream = new ByteArrayInputStream(is);
    • ByteArrayInputStream 只接受 byte[] 作为输入,而不接受 inputStream 所以你想让我从 inputstream 中读取字节,制作一个 byte[],然后把它放到另一个流中?这对我来说毫无意义......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 2017-01-04
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多