Hi All,
I have downloaded image from web and displayed in imageview,which works fine in Emulator and device.when i supposed to run on android 2.2 real device ,it throws null pointer Exception. what is the reason? I have used the following code..
public Bitmap downloadImage(String url) { Bitmap bm = null; try { URL myURL = new URL(url); final BufferedInputStream bis = new BufferedInputStream(myURL .openStream(), 1024); final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); BufferedOutputStream out = new BufferedOutputStream(dataStream, 1024); copy(bis, out); out.flush(); final byte[] data = dataStream.toByteArray(); BitmapFactory.Options bfo = new BitmapFactory.Options(); bfo.inSampleSize = 2; bm = BitmapFactory.decodeByteArray(data, 0, data.length, bfo); bis.close(); } catch (Exception e) { } return bm; }
static final int BUFF_SIZE = 1024;
static final byte[] buffer = new byte[BUFF_SIZE];
public static void copy(BufferedInputStream in, BufferedOutputStream out)
throws IOException {
try {
while (true) {
synchronized (buffer)
{
int amountRead = in.read(buffer);
if (amountRead == -1) {
break;
}
out.write(buffer, 0, amountRead);
}
}
} catch (Exception e) {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}