|
20 | 20 | import android.graphics.Bitmap;
|
21 | 21 | import android.graphics.BitmapFactory;
|
22 | 22 | import android.text.style.ImageSpan;
|
| 23 | +import android.util.TypedValue; |
23 | 24 |
|
24 | 25 | import androidx.annotation.NonNull;
|
25 | 26 | import androidx.annotation.Nullable;
|
|
35 | 36 | import org.quantumbadger.redreader.common.General;
|
36 | 37 | import org.quantumbadger.redreader.common.GenericFactory;
|
37 | 38 | import org.quantumbadger.redreader.common.Optional;
|
| 39 | +import org.quantumbadger.redreader.common.PrefsUtility; |
38 | 40 | import org.quantumbadger.redreader.common.Priority;
|
39 | 41 | import org.quantumbadger.redreader.common.datastream.SeekableInputStream;
|
40 | 42 | import org.quantumbadger.redreader.http.FailedRequestBody;
|
@@ -149,10 +151,33 @@ public void onDataStreamComplete(
|
149 | 151 | try (InputStream is = stream.create()) {
|
150 | 152 | image = BitmapFactory.decodeStream(is);
|
151 | 153 |
|
152 |
| - image = Bitmap.createScaledBitmap(image, |
153 |
| - image.getWidth() / 2, |
154 |
| - image.getHeight() / 2, |
155 |
| - true); |
| 154 | + if (image == null) { |
| 155 | + throw new IOException("Failed to decode bitmap"); |
| 156 | + } |
| 157 | + |
| 158 | + final int textSize = 11; |
| 159 | + final float maxImageHeightMultiple = 2.0F; |
| 160 | + |
| 161 | + final float maxHeight = TypedValue.applyDimension( |
| 162 | + TypedValue.COMPLEX_UNIT_SP, |
| 163 | + PrefsUtility.appearance_fontscale_comment_headers() |
| 164 | + * textSize |
| 165 | + * maxImageHeightMultiple, |
| 166 | + activity.getApplicationContext() |
| 167 | + .getResources() |
| 168 | + .getDisplayMetrics()); |
| 169 | + |
| 170 | + if (image.getHeight() > maxHeight) { |
| 171 | + final float imageAspectRatio = |
| 172 | + (float) image.getHeight() / image.getWidth(); |
| 173 | + |
| 174 | + final float newImageWidth = maxHeight / imageAspectRatio; |
| 175 | + |
| 176 | + image = Bitmap.createScaledBitmap(image, |
| 177 | + Math.round(newImageWidth), |
| 178 | + Math.round(maxHeight), |
| 179 | + true); |
| 180 | + } |
156 | 181 |
|
157 | 182 | if (image == null) {
|
158 | 183 | throw new IOException("Failed to decode bitmap");
|
|
0 commit comments