在我的应用程序中,我对图像文件发出网络请求,然后尝试减少生成的位图大小。我正在构建this文档。我有一个文件流要读取并解码为位图,而不是静态资源。正如您在下面的代码中看到的那样,我使用的是BufferredInputStream
,因此在第二次调用decodeStream
之前,我可以重置流。但是我在该语句之后记录的消息没有被打印,也没有创建位图。它甚至没有任何错误。以下是我如何从URL中读取和解码图像 -
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inp = connection.getInputStream();
BufferedInputStream bufferedinp = new BufferedInputStream(inp);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
_BitMap = BitmapFactory.decodeStream(bufferedinp, null, options);
Log.d(TAG,"imageanalysis init width: " + Integer.toString(options.outWidth));
Log.d(TAG,"imageanalysis init height: " + Integer.toString(options.outHeight));
// get system height and width here -- fixed for now
int reqWidth = 250;
int reqHeight = 220;
options.inSampleSize = Common.getInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
bufferedinp.reset(); // resetting the stream to read from start
_BitMap = BitmapFactory.decodeStream(bufferedinp, null, options);
Log.d(TAG,"imageanalysis fin width: " + Integer.toString(_BitMap.getWidth())); // doesn't get logged
Log.d(TAG,"imageanalysis fin height" + Integer.toString(_BitMap.getHeight())); // doesn't get logged
仅供参考,getInSampleSize
在这里工作正常(在我的情况下返回2) -
public static int getInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if(height > reqHeight || width > reqWidth){
final int halfHeight = height / 2;
final int halfWidth = width / 2;
Log.d("Common", "imageananalysis halfHeight: " + Integer.toString(halfHeight));
Log.d("Common", "imageananalysis halfWidth: " + Integer.toString(halfWidth));
while((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth)
{
Log.d("Common", "imageanalysis in loop samplesize: " + Integer.toString(inSampleSize));
inSampleSize *= 2;
}
}
Log.d("Common", "imageanalysis ret samplesize: " + Integer.toString(inSampleSize));
return inSampleSize;
}
这里有什么问题?任何帮助将不胜感激。