【发布时间】: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