android - BitmapFactory.decodeStream(is) fails on some image in Download folder -
i'm using phonegap 2.5.0 , here how call method:
try { inputstream = cordova.getactivity().getcontentresolver() .openinputstream(uri.parse(inputstring)); bitmap bmp = bitmapfactory.decodestream(is); is.close();
the code works fine when take photo using camera randomly fails on images download folder. checked images, downloaded locally url content://media/external/images/media/xxxx. files pretty large 6mb while others small 700k. failure seems random returning null , not caught exception.
any chance it's jpeg?
see known issue:-
https://code.google.com/p/android/issues/detail?id=6066
i use following decoding bitmaps:-
bitmapfactory.decodestream(new flushedinputstream(is), null, opts); public class flushedinputstream extends filterinputstream { public flushedinputstream(inputstream inputstream) { super(inputstream); } @override public long skip(long n) throws ioexception { long totalbytesskipped = 0l; while (totalbytesskipped < n) { long bytesskipped = in.skip(n - totalbytesskipped); if (bytesskipped == 0l) { int mybyte = read(); if (mybyte < 0) { break; // reached eof } else { bytesskipped = 1; // read 1 byte } } totalbytesskipped += bytesskipped; } return totalbytesskipped; } }
also, if of images large, may want set sample size don't cause large allocation.
bitmapfactory.options opts = new bitmapfactory.options(); opts.insamplesize = samplesize;
where samplesize sensible value you've calculated.
Comments
Post a Comment