将其下载到字节数组并解码字节数组:
byte[] data = read(inputStreamFromConnection);
if (data != null) {
Bitmap downloadedBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
}
public static byte[] read(InputStream is) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
try {
// Read buffer, to read a big chunk at a time.
byte[] buf = new byte[2048];
int len;
// Read until -1 is returned, i.e. stream ended.
while ((len = is.read(buf)) != -1) {
baos.write(buf, 0, len);
}
} catch (IOException e) {
Log.e("Downloader", "File could not be downloaded", e);
} finally {
try {
is.close();
} catch (IOException e) {
// Input stream could not be closed.
}
}
return baos.toByteArray();
}