如何调整通过URL检索的图像并保存?

时间:2012-09-03 09:06:42

标签: android

我通过互联网上的URL获取图像,并在保存之前尝试调整大小(缩小尺寸)。我设法保存它,但我无法调整它。我怎么能这样做?这是代码:

URL url = new URL(LogoURL);

InputStream input = url.openStream();
try {
    OutputStream output = new FileOutputStream("data/data/com.android.mylogo/logo.jpg");
    try {
        //byte[] buffer = new byte[aReasonableSize];     
        int bytesRead = 0;
        System.out.println("Buffer Length is \t:-" + buffer.length);
        while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
            System.out.println("inside while");
            output.write(buffer, 0, bytesRead);
        }
    } finally {
        output.close();
        System.out.println("saved image");
    }
} finally {
    input.close();
}

2 个答案:

答案 0 :(得分:0)

无论如何都要压缩JPG,因此无需再尝试压缩它们。 通常,要压缩某些内容,请查看java.util.zip包的类。

答案 1 :(得分:0)

如果要将图像缩小到特定尺寸,可以使用以下代码:

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(LogoURL);

try {
    HttpResponse response = client.execute(request);
    final int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode != HttpStatus.SC_OK) {
        Log.w(LOG_TAG, "Error " + statusCode + " while retrieving bitmap from " + url);
        return null;
    }

    HttpEntity entity = response.getEntity();
    if (entity != null) {

        InputStream is = null;
        BufferedInputStream bis = null;
        try {
            is = url.openStream();
            bis = new BufferedInputStream(is);

            int sampleSize = 1;

            bis.mark(Integer.MAX_VALUE);

            Options bounds = new Options();
            bounds.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(bis, null, bounds);
            if (bounds.outWidth != -1) {
                int width = bounds.outWidth;
                int height = bounds.outHeight;
                boolean withinBounds = width <= YOUR_DESIRED_WIDTH && height <= YOUR_DESIRED_HEIGHT;

                int newWidth = width;
                int newHeight = height;
                while (!withinBounds) {
                    newWidth /= 2;
                    newHeight /= 2;
                    sampleSize *= 2;
                    withinBounds = newWidth <= YOUR_DESIRED_WIDTH && newHeight <= YOUR_DESIRED_HEIGHT;
                }
            } else {
                Log.w(LOG_TAG, "Can't open bitmap at " + url);
                return null;
            }

            try {
                bis.reset();
            } catch (IOException e) {
                if(is != null){
                    is.close();
                }

                if(bis != null){
                    bis.close();
                }

                if(!entity.isRepeatable()){
                    entity.consumeContent();
                    response = client.execute(request);
                    entity = response.getEntity();
                }

                is = entity.getContent();
                bis = new BufferedInputStream(is);
            }

            Options opts = new Options();
            opts.inSampleSize = sampleSize;
            Bitmap bm = BitmapFactory.decodeStream(bis, null, opts);

            return bm;
        } finally {
            if (is != null) {
                is.close();
            }               

            if (bis != null) {
                bis.close();
            }

            entity.consumeContent();
        }
    }
} catch (IOException e) {
    request.abort();
    Log.w(LOG_TAG, "I/O error while retrieving bitmap from " + url, e);
} catch (IllegalStateException e) {
    request.abort();
    Log.w(LOG_TAG, "Incorrect URL: " + url);
} catch (Exception e) {
    request.abort();
    Log.w(LOG_TAG, "Error while retrieving bitmap from " + url, e);
}

使用Options bounds = new Options(); bounds.inJustDecodeBounds = true;打开图像时,将不会下载图像数据,仅下载图像的大小。我使用这个尺寸来计算新的比例,以获得所需的宽度和高度。

使用选项Options opts = new Options(); opts.inSampleSize = sampleSize;,BitmapFactory将下载已调整大小的图像。以这种方式节省内存和带宽。

请注意,sampleSize值应为2的幂。它也适用于不同的数字,但效率更高。