ability to open files from media preview. fixes #3521

This commit is contained in:
Daniel Gultsch 2019-09-29 02:00:06 +02:00
parent 618d892ae7
commit aef394c9f0
2 changed files with 27 additions and 1 deletions

View File

@ -193,6 +193,14 @@ public class FileBackend {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/Camera/"; 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) { public static Uri getUriForFile(Context context, File file) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE) {
try { try {

View File

@ -1,17 +1,21 @@
package eu.siacs.conversations.ui.adapter; package eu.siacs.conversations.ui.adapter;
import android.content.ActivityNotFoundException;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
import android.databinding.DataBindingUtil; import android.databinding.DataBindingUtil;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.Toast;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
@ -20,6 +24,7 @@ import java.util.concurrent.RejectedExecutionException;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.databinding.MediaPreviewBinding; import eu.siacs.conversations.databinding.MediaPreviewBinding;
import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.ui.ConversationFragment; import eu.siacs.conversations.ui.ConversationFragment;
import eu.siacs.conversations.ui.XmppActivity; import eu.siacs.conversations.ui.XmppActivity;
import eu.siacs.conversations.ui.util.Attachment; import eu.siacs.conversations.ui.util.Attachment;
@ -54,11 +59,24 @@ public class MediaPreviewAdapter extends RecyclerView.Adapter<MediaPreviewAdapte
MediaAdapter.renderPreview(context, attachment, holder.binding.mediaPreview); MediaAdapter.renderPreview(context, attachment, holder.binding.mediaPreview);
} }
holder.binding.deleteButton.setOnClickListener(v -> { holder.binding.deleteButton.setOnClickListener(v -> {
int pos = mediaPreviews.indexOf(attachment); final int pos = mediaPreviews.indexOf(attachment);
mediaPreviews.remove(pos); mediaPreviews.remove(pos);
notifyItemRemoved(pos); notifyItemRemoved(pos);
conversationFragment.toggleInputMethod(); 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<Attachment> attachments) { public void addMediaPreviews(List<Attachment> attachments) {