BitmapFactory.decodeStream gibt null zurück, wenn Optionen festgelegt sind
Ich habe Probleme mitBitmapFactory.decodeStream(inputStream)
. Wenn Sie es ohne Optionen verwenden, wird ein Bild zurückgegeben. Aber wenn ich es mit Optionen wie in.decodeStream(inputStream, null, options)
Bitmaps werden nie zurückgegeben.
Ich versuche, eine Bitmap herunterzusampeln, bevor ich sie tatsächlich lade, um Speicherplatz zu sparen. Ich habe einige gute Anleitungen gelesen, aber keine mit.decodeStream
.
WERKE NUR FEIN
URL url = new URL(sUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is, null, options);
FUNKTIONIERT NICHT
InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is, null, ,options);
InputStream is = connection.getInputStream();
Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, options);
Boolean scaleByHeight = Math.abs(options.outHeight - TARGET_HEIGHT) >= Math.abs(options.outWidth - TARGET_WIDTH);
if (options.outHeight * options.outWidth * 2 >= 200*100*2){
// Load, scaling to smallest power of 2 that'll get it <= desired dimensions
double sampleSize = scaleByHeight
? options.outHeight / TARGET_HEIGHT
: options.outWidth / TARGET_WIDTH;
options.inSampleSize =
(int)Math.pow(2d, Math.floor(
Math.log(sampleSize)/Math.log(2d)));
}
// Do the actual decoding
options.inJustDecodeBounds = false;
Bitmap img = BitmapFactory.decodeStream(is, null, options);