适用于较大图像的Android Image Getter

y r*_*rao 2 android thumbnails

我已经使用了所有与标准网络相关的代码来获取图像45KB to 75KB但是所有这些代码都失败了这些方法适用于大约3-5KB图像大小的文件.我如何实现下载图像,45 - 75KB以便在我的网络操作中在Android上的ImageView上显示它们我使用过的东西

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();
Run Code Online (Sandbox Code Playgroud)

我使用的第二个选项是::

DefaultHttpClient httpClient = new DefaultHttpClient();

HttpGet getRequest = new HttpGet(urlString);

HttpResponse response = httpClient.execute(getRequest);
Run Code Online (Sandbox Code Playgroud)

为什么此代码适用于较小尺寸的图像而不适用于较大尺寸的图像.?

Rob*_*oss 7

您正在下载的图像大小非常无关紧要.它使用BitmapFactory.decodeStream解码的大小是您处理图像所需的内存.因此,重新采样可能很有用.

    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();
Run Code Online (Sandbox Code Playgroud)