views:

443

answers:

2

I have used all the Standard Network related code for Getting Images of about 45KB to 75KB but all are failing these methods work fine for Files of about 3-5KB size of Images. How can I achieve Downloading Image of 45 - 75KB for displaying them on an ImageView in Android for my Network Operations the Things I have used are

final URL url = new URL(urlString);

final URLConnection conn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) conn;

httpConn.setAllowUserInteraction(true);

httpConn.setInstanceFollowRedirects(true);

httpConn.setRequestMethod("GET");

httpConn.connect();

and the Second option that I have had used is::

DefaultHttpClient httpClient = new DefaultHttpClient();

HttpGet getRequest = new HttpGet(urlString);

HttpResponse response = httpClient.execute(getRequest);

why is this code functional for Smaller Sized Images and not for Larger Size Images. ?

+2  A: 

It would be nice to see what kind of code you use for decoding the response into a bitmap. Anyway, try using a BufferedInputStream like this:

public Bitmap getRemoteImage(final URL aURL) { 
  try { 
    final URLConnection conn = aURL.openConnection(); 
    conn.connect(); 
    final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); 
    final Bitmap bm = BitmapFactory.decodeStream(bis); 
    return bm; 
  } catch (IOException e) { 
    Log.d("DEBUGTAG", "Oh noooz an error..."); 
  } 
  return null; 
}

And yes, please start working on your accept-rate

PHP_Jedi
Ya PHP_Jedi BufferedInputStream is really what i thought of this is really something that would solve the problem and yes thanks for helping man.
y ramesh rao
+3  A: 

The size of the image you are downloading is pretty irrelevant. The size it decodes with BitmapFactory.decodeStream, however is the memory you are gone need to handle the image. Therefore a reSampling might be useful.

    Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    BitmapFactory.decodeStream(is, null, options);

    Boolean scaleByHeight = Math.abs(options.outHeight - TARGET_HEIGHT) >= Math.abs(options.outWidth - TARGET_WIDTH);

    if(options.outHeight * options.outWidth >= 200*200){
    // Load, scaling to smallest power of 2 if dimensions >= desired dimensions
    double sampleSize = scaleByHeight
            ? options.outHeight / TARGET_HEIGHT
            : options.outWidth / TARGET_WIDTH;
    options.inSampleSize = 
          (int)Math.pow(2d, Math.floor(
          Math.log(sampleSize)/Math.log(2d)));
    }

    // Do the actual decoding
    options.inJustDecodeBounds = false;

    is.close();
    is = getHTTPConnectionInputStream(sUrl);
    Bitmap img = BitmapFactory.decodeStream(is, null, options);
    is.close();
Robert Foss
Robert Foss the size i'm talking here is not a the image bounded size but the size of the image on the server and the issue is that while downloading the image of 40K the download becomes unsuccessful and unfruitful. anything below that the same code suffices the matter. that is what is my issue is.
y ramesh rao
Thanks.. this was extremely helpful!
Tim H
Hi Robert!What the " 200*200*2" mean?
Francesco
It was a while since I wrote this. 200*200 is the largest size allowed for the picture before it will do any resizing.options.outHeight * options.outWidth * 2 >= 200*200*2Should be changed into: options.outHeight * options.outWidth >= 200*200Dont know how that extra *2 snuck its way there :PRemoved it in the soulution.
Robert Foss