I am trying to download imags from url and then decrypt them. The problem is that I don’t know how big they are, and if I decode them right away, the application will come out with too large images.
I do the following, and it works with most images, but with some of them it throws an exception java.io.IOException: Mark has been invalidated. This is not a matter of size, because it happens with 75 KB or 120 KB images, not 20 MB or 45 KB images. Also, the format is not important, as this can happen with either jpg or png image.
pisis InputStream.
Options opts = new BitmapFactory.Options();
BufferedInputStream bis = new BufferedInputStream(pis);
bis.mark(1024 * 1024);
opts.inJustDecodeBounds = true;
Bitmap bmImg=BitmapFactory.decodeStream(bis,null,opts);
Log.e("optwidth",opts.outWidth+"");
try {
bis.reset();
opts.inJustDecodeBounds = false;
int ratio = opts.outWidth/800;
Log.e("ratio",String.valueOf(ratio));
if (opts.outWidth>=800)opts.inSampleSize = ratio;
return BitmapFactory.decodeStream(bis,null,opts);
} catch (IOException e) {
e.printStackTrace();
return null;
}
sergi source
share