From aef394c9f0e6e82d522bb43809f6d60e5c739206 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 29 Sep 2019 02:00:06 +0200 Subject: [PATCH] ability to open files from media preview. fixes #3521 --- .../persistance/FileBackend.java | 8 ++++++++ .../ui/adapter/MediaPreviewAdapter.java | 20 ++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index fe6530b66..37168016a 100644 --- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java @@ -193,6 +193,14 @@ public class FileBackend { return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/Camera/"; } + public static Uri getUriForUri(Context context, Uri uri) { + if ("file".equals(uri.getScheme())) { + return getUriForFile(context, new File(uri.getPath())); + } else { + return uri; + } + } + public static Uri getUriForFile(Context context, File file) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE) { try { diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MediaPreviewAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MediaPreviewAdapter.java index 405ddcb41..a2f423a04 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MediaPreviewAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MediaPreviewAdapter.java @@ -1,17 +1,21 @@ package eu.siacs.conversations.ui.adapter; +import android.content.ActivityNotFoundException; import android.content.Context; +import android.content.Intent; import android.content.res.Resources; import android.databinding.DataBindingUtil; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; +import android.net.Uri; import android.os.AsyncTask; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.ViewGroup; import android.widget.ImageView; +import android.widget.Toast; import java.lang.ref.WeakReference; import java.util.ArrayList; @@ -20,6 +24,7 @@ import java.util.concurrent.RejectedExecutionException; import eu.siacs.conversations.R; import eu.siacs.conversations.databinding.MediaPreviewBinding; +import eu.siacs.conversations.persistance.FileBackend; import eu.siacs.conversations.ui.ConversationFragment; import eu.siacs.conversations.ui.XmppActivity; import eu.siacs.conversations.ui.util.Attachment; @@ -54,11 +59,24 @@ public class MediaPreviewAdapter extends RecyclerView.Adapter { - int pos = mediaPreviews.indexOf(attachment); + final int pos = mediaPreviews.indexOf(attachment); mediaPreviews.remove(pos); notifyItemRemoved(pos); conversationFragment.toggleInputMethod(); }); + holder.binding.mediaPreview.setOnClickListener(v -> view(context, attachment)); + } + + private static void view(final Context context, Attachment attachment) { + final Intent view = new Intent(Intent.ACTION_VIEW); + final Uri uri = FileBackend.getUriForUri(context, attachment.getUri()); + view.setDataAndType(uri, attachment.getMime()); + view.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + try { + context.startActivity(view); + } catch (ActivityNotFoundException e) { + Toast.makeText(context, R.string.no_application_found_to_open_file, Toast.LENGTH_SHORT).show(); + } } public void addMediaPreviews(List attachments) {