【问题标题】:Download Streamed gif file and store in byte array下载流式 gif 文件并存储在字节数组中
【发布时间】:2017-01-24 01:24:14
【问题描述】:

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

    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 进行转换,但返回空值。

【问题讨论】:

    标签: android inputstream gif


    【解决方案1】:

    好吧,经过一番谷歌搜索后,我得到了这个工作。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-04
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      • 2014-12-07
      • 2017-09-18
      • 2019-04-09
      • 1970-01-01
      相关资源
      最近更新 更多