Fix preview image height/width

Signed-off-by: Balazs Toldi <balazs@toldi.eu>
This commit is contained in:
Balazs Toldi 2023-07-21 16:00:53 +02:00
parent 54f7980880
commit a784ca86f9
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58

View File

@ -2,15 +2,22 @@ package eu.toldi.infinityforlemmy.post;
import static java.lang.Integer.max; import static java.lang.Integer.max;
import android.graphics.BitmapFactory;
import android.net.Uri; import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.os.StrictMode;
import android.text.Html; import android.text.Html;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
@ -161,8 +168,10 @@ public class ParsePost {
String distinguished = ""; String distinguished = "";
String suggestedSort = ""; String suggestedSort = "";
ArrayList <Post.Preview> previews = new ArrayList<>(); ArrayList <Post.Preview> previews = new ArrayList<>();
if(!post.isNull("thumbnail_url")){ if(!post.isNull("thumbnail_url")) {
previews.add(new Post.Preview(post.getString("thumbnail_url"),0,0,"","")); String thumbnail = post.getString("thumbnail_url");
int[] wh_array = getImageDimension(thumbnail);
previews.add(new Post.Preview(thumbnail, wh_array[0], wh_array[1], "", ""));
} }
@ -662,11 +671,41 @@ public class ParsePost {
public interface ParsePostListener { public interface ParsePostListener {
void onParsePostSuccess(Post post); void onParsePostSuccess(Post post);
void onParsePostFail(); void onParsePostFail();
} }
public interface ParseRandomPostListener { public interface ParseRandomPostListener {
void onParseRandomPostSuccess(String postId, String subredditName); void onParseRandomPostSuccess(String postId, String subredditName);
void onParseRandomPostFailed(); void onParseRandomPostFailed();
} }
public static int[] getImageDimension(String imageUrl) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy); //Permit all for simplicity. You may want to revise this for your actual app.
HttpURLConnection urlConnection = null;
try {
URL url = new URL(imageUrl);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoInput(true);
urlConnection.connect();
InputStream input = urlConnection.getInputStream();
//Just decode image size, not the image itself
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(input, null, options);
return new int[]{options.outWidth, options.outHeight};
} catch (IOException e) {
Log.e("getImageDimension", "Error reading image dimensions", e);
return new int[]{0, 0};
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
} }