In my app, I make a network request for the image file and then try to reduce the generated bitmap size. I am building upon this documentation. Instead of a static resource, I have a file stream to be read and decoded to bitmap. As you can see in below code, I am using BufferredInputStream
, so that before calling decodeStream
second time, I can reset the stream. But the messages which I am logging after that statement are not getting printed and also no bitmap is created. It does not even throw any error. Here is how I am reading and decoding image from a 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
Just for reference, getInSampleSize
is here and works fine (returns 2 in my case) -
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;
}
What can be the issue here? Any help will be appreciated.