Android从webservice获取图片,如何?

标签 android bitmap arrays

我有一个 android 应用程序需要从网络服务接收几张图片。 但是如何做到这一点呢?

在我的网络服务中,我目前只发送 1 张图像作为 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;
        }
    }

byte[] 是从我的网络服务返回的。 但在我的 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;
 }

谁能帮我解决这个问题?

最佳答案

假设传入的bytearray是一个字节数组,

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 的转换代码

关于Android从webservice获取图片,如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8786909/

相关文章:

android - 相机预览始终返回 320x240 位图

python - 重新排序 numpy ndarray 的最后一个维度

javascript - 获取数组中的差异

java - Android:将一些只读字节传递给 native C++ 的最有效方法

java - Android Proguard - 一步一步

android - 从多个复选框中选择一个复选框并让用户选中复选框

android - 从 Android 设备获取收件箱消息以显示在自定义 ListView 中

qt - 在 Qt 中使用像素图或位图快速绘图

ios - 有关 Swift 上 iOS 中 drawRect 的性能问题

python - 如何将 numpy 数组中的值组织到包含特定范围值的容器中?