Tôi đang gặp sự cố với BitmapFactory.decodeStream(inputStream)
. Khi sử dụng nó mà không có tùy chọn, nó sẽ trả về một hình ảnh. Nhưng khi tôi sử dụng nó với các tùy chọn như trong .decodeStream(inputStream, null, options)
nó không bao giờ trả về bitmap.BitmapFactory.decodeStream trả về null khi các tùy chọn được đặt
Những gì tôi đang cố gắng làm là downsample một Bitmap trước khi tôi thực sự tải nó để tiết kiệm bộ nhớ. Tôi đã đọc một số hướng dẫn tốt, nhưng không có hướng dẫn nào sử dụng .decodeStream
.
làm việc tốt
URL url = new URL(sUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is, null, options);
KHÔNG LÀM VIỆC
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);
Đầu ra từ câu lệnh System.out.println ("Samplesize:" ...) của bạn là gì? Chỉ ra rằng options.inSampleSize là một giá trị chấp nhận được? –
Có, nó trả về giá trị chấp nhận được mỗi lần. –
Đã xóa tuyên bố do lỗi đang được gỡ lỗi. –