开发者

Android Image Getter for Larger Images

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. ?


The size of the image you are downloading is pretty irrelevant. The size it decodes with BitmapFactory.decodeStream, however is the memory you are going to 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();


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; 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜