【问题标题】:Unable to draw a bitmap as it is downloaded on a canvas下载到画布上时无法绘制位图
【发布时间】:2016-01-14 14:06:59
【问题描述】:

我正在从网站下载一张图片,该图片将显示在 imageview 上。

我希望图像在下载时显示在图像视图中,并且不要等待整个图像下载完成。我的意思是如果图像是 10000 字节并且要下载的第一个字节是 1024,那么应该从 1024 字节构造图像并形成下载图像的一部分并将其绘制在画布上

byte[] buffer =new byte[1024];
                int bytesRead = -1;
                byte[] writtenOnes;



                while((bytesRead = inputStream.read(buffer)) != -1){
                    fileOutputStream.write(buffer, 0, bytesRead);
                    fileOutputStream.flush();
                    filebytes = Files.toByteArray(media);
                    status = (100 * filebytes.length) / contentLength;
                    publishProgress(status);

                }
                fileOutputStream.flush();

                fileOutputStream.close();
                inputStream.close();

                if( media.length() == contentLength){
                    d(" WELL DONE, FILECOMPLETLY DOWNLOADED. SIZE IS "+ contentLength);
                }else{
                    d(" file not completely downloaded. bytes remaining "+(contentLength -media.length()));
                }
            }
            connection.disconnect();
            return  mediaFile;
        }catch(Exception exc){
            exc.printStackTrace();
            return "";
        }

    }

    protected void onProgressUpdate(Integer... status) {
        int state = status[0];
        incompleteBitmap =BitmapFactory.decodeByteArray(filebytes, 0, filebytes.length);

        d(" status is "+ state);
        canvas.drawBitmap(incompleteBitmap,0,0,new Paint(Paint.FILTER_BITMAP_FLAG));
        progressBar.setProgress(state);
    }

我无法从下载的字节创建位图。当我尝试在onProgressUpdate() 方法上获取形成的位图时,我得到一个 NullpointerException

问:有什么方法可以实现吗?

【问题讨论】:

    标签: android image canvas bitmap android-imageview


    【解决方案1】:

    如果图像在您的域内,我使用支持渐进式 JPEG 格式的 Fresco 图像库取得了很好的效果。但是,如果您正在寻找完整的原版解决方案或已经在使用另一个图像视图库,或者图像和格式超出您的控制范围,这可能是不可取的:

    Fresco progressive JPEG documentation

    【讨论】:

    • 一切都在我的控制之下,包括图像格式和获取图像的服务器。让我试试你的解决方案,我会告诉你结果
    • 我在以编程方式设置我的 simpledraweeview 的宽度和高度时遇到问题。而且我在GenericDraweeHierachy 类中看不到任何方法,当我使用android 方法时,就好像它们没有被实现。 simpledrawee.getLayoutParams().width=350。你遇到过这样的问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多