我正在构建一个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进行转换,但返回null。
答案 0 :(得分: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();