Added PDF previews

pull/214/head
M66B 11 months ago
parent 415c5d189b
commit f913347455

@ -23,11 +23,15 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.AnimatedImageDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.pdf.PdfRenderer;
import android.os.Build;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
@ -60,6 +64,8 @@ public class AdapterImage extends RecyclerView.Adapter<AdapterImage.ViewHolder>
private List<EntityAttachment> items = new ArrayList<>();
private static final int PDF_WIDTH = 120;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
private View view;
private ImageView ivImage;
@ -108,6 +114,22 @@ public class AdapterImage extends RecyclerView.Adapter<AdapterImage.ViewHolder>
String type = args.getString("type");
int max = args.getInt("max");
if ("application/pdf".equals(type)) {
// https://medium.com/@aditya09tyagi/android-pdf-reader-using-pdfrenderer-6daa2dacec1a
try (ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)) {
try (PdfRenderer pdf = new PdfRenderer(pfd)) {
try (PdfRenderer.Page page = pdf.openPage(0)) {
int width = Helper.dp2pixels(context, PDF_WIDTH);
int height = (int) ((float) width / page.getWidth() * page.getHeight());
Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bm);
canvas.drawColor(Color.WHITE);
page.render(bm, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
return new BitmapDrawable(context.getResources(), bm);
}
}
}
} else {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean webp = prefs.getBoolean("webp", true);
@ -148,6 +170,7 @@ public class AdapterImage extends RecyclerView.Adapter<AdapterImage.ViewHolder>
return null;
return new BitmapDrawable(context.getResources(), bm);
}
}
@Override
protected void onExecuted(Bundle args, Drawable image) {

@ -3650,9 +3650,10 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
List<EntityAttachment> images = new ArrayList<>();
if (thumbnails && bind_extras) {
for (EntityAttachment attachment : attachments)
if (attachment.isAttachment() && attachment.isImage()) {
if (attachment.isPDF() ||
(attachment.isAttachment() && attachment.isImage())) {
images.add(attachment);
if (attachment.available)
if (attachment.available && !attachment.isPDF())
iavailable++;
}
}

@ -117,6 +117,10 @@ public class EntityAttachment {
return ImageHelper.isImage(getMimeType());
}
boolean isPDF() {
return "application/pdf".equals(getMimeType());
}
boolean isCompressed() {
if ("application/zip".equals(type))
return true;

Loading…
Cancel
Save