我正在使用以下代码从URL下载位图:
myFileUrl =新的URL(fileUrl);
HttpURLConnection conn =(HttpURLConnection)myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream是= conn.getInputStream();
位图bmpTemp = BitmapFactory.decodeStream(is);
但有时候,我的意思是,位图一次等于100!任何人都知道可能是什么问题,
谢谢
最佳答案
网址连接中有错误。它具有使用FlushedInputStream装饰器函数的修复程序。
这是它的代码:
/*
* An InputStream that skips the exact number of bytes provided, unless it reaches EOF.
*/
public static class FlushedInputStream extends FilterInputStream {
public FlushedInputStream(InputStream inputStream) {
super(inputStream);
}
@Override
public long skip(long n) throws IOException {
long totalBytesSkipped = 0L;
while (totalBytesSkipped < n) {
long bytesSkipped = in.skip(n - totalBytesSkipped);
if (bytesSkipped == 0L) {
int b = read();
if (b < 0) {
break; // we reached EOF
} else {
bytesSkipped = 1; // we read one byte
}
}
totalBytesSkipped += bytesSkipped;
}
return totalBytesSkipped;
}
}
用法是:
BitmapFactory.decodeStream(new FlushedInputStream(is));