2016-09-15 79 views
0

我正在构建一个android应用程序,我需要从一个URL下载一个gif图像并存储在一个字节数组中。一直使用位图工厂类,但它一直返回null。我将字节数组存储到sqlite然后检索并显示。下载Streamed gif文件并存储在字节数组中

urlConnection = (HttpURLConnection) url.openConnection(); 

     InputStream is = urlConnection.getInputStream(); 


     BufferedInputStream bis = new BufferedInputStream(is); 
     ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 
     //We create an array of bytes 
     byte[] data = new byte[4094]; 
     int current = 0; 

     while((current = bis.read(data,0,data.length)) != -1){ 
      buffer.write(data,0,current); 
     } 

我甚至试图转换使用Bitmap.Factory,但返回空值。

回答

0

确定后,有一点谷歌搜索我得到这个工作。

URLConnection ucon = url.openConnection(); 


     InputStream is = ucon.getInputStream(); 

     BufferedInputStream bis = new BufferedInputStream(is); 


     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 


     byte[] img = new byte[5000]; 

     int current = 0; 


     while ((current = bis.read()) != -1) { 
      baos.write(current); 
     } 



     forecastJsonStr = baos.toByteArray();