简体   繁体   English

在android中调整网络图像的大小?

[英]Resizing an image from the network in android?

I'm trying to adapt the code in google developer guides to resize a large image obtained from HTTP. 我正在尝试调整Google开发人员指南中的代码,以调整从HTTP获取的大图像的大小。 In order to resize the image, I have to process it once (using Bitmapfactory.decodeStream) to determine its original height and width. 为了调整图像大小,我必须处理一次(使用Bitmapfactory.decodeStream)来确定其原始高度和宽度。 Then, I have to run Bitmapfactory.decodeStream again in order to resize it. 然后,我必须再次运行Bitmapfactory.decodeStream以调整它的大小。 THe problem with this approach is that I cannot use the same stream twice. 这种方法的问题是我不能两次使用相同的流。 If I do, the second called to decodeStream returns null. 如果我这样做,第二次调用decodeStream会返回null。

I thought about trying to clone / copy the stream first so that I would have two copies to work with. 我想过首先尝试克隆/复制流,以便我可以使用两个副本。 However, this uses up memory, which was the problem I was trying to solve by resize the image, in the first place. 但是,这会消耗内存,这是我试图通过调整图像大小来解决的问题。

Just use the Bitmap returned by Bitmapfactory.decodeStream() for the resize operation. 只需使用Bitmapfactory.decodeStream()返回的Bitmap进行调整大小操作即可。 You do not need to decode it twice. 您不需要对其进行两次解码。 You have it already. 你已经拥有它了。

Bitmap b = Bitmapfactory.decodeStream(/* your InputStream */);
// get original dimensions from b
int h = b.getHeight();
int w = b.getWidth();
// resize b to half (actually quarter) size
Bitmap resizedBitmap = Bitmap.createScaledBitmap(b, w/2, h/2, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM