I am using following method to get bitmap from url but image is not displayed. Image is a png file about 50x50px. Thank you.
public static Bitmap getBitmapFromURL(String src) {
try {
Log.e("src",src);
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(input,null,options);
Log.e("Bitmap","returned");
//The new size we want to scale to
final int REQUIRED_SIZE=70;
//Find the correct scale value.
int scale=1;
while(options.outWidth/scale/2>=REQUIRED_SIZE && options.outHeight/scale/2>=REQUIRED_SIZE)
scale*=2;
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(input, null, o2);
} catch (IOException e) {
e.printStackTrace();
Log.e("Exception",e.getMessage());
return null;
}
}
Your
InputStreamis “eaten away”. You can use a stream only once and hence when you read the bitmap headers in order to scale the image your input stream gets ‘eaten away’. You need to create a new InputStream in order to get the real bitmap.