From 1dc55f72e3212ae2b317c87c421aaa32a7a80950 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 4 Sep 2016 22:59:40 +0200 Subject: don't use fileprovider when opening files on android M and below --- .../conversations/ui/adapter/MessageAdapter.java | 24 +++++++++++++++------- src/main/res/values/strings.xml | 1 + 2 files changed, 18 insertions(+), 7 deletions(-) (limited to 'src/main') diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java index bc706cda..6f536983 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -11,6 +11,7 @@ import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.AsyncTask; +import android.os.Build; import android.support.v4.content.FileProvider; import android.text.Spannable; import android.text.SpannableString; @@ -716,21 +717,30 @@ public class MessageAdapter extends ArrayAdapter { if (mime == null) { mime = "*/*"; } - Uri uri = FileProvider.getUriForFile(activity,"eu.siacs.conversations.files",file); + Uri uri; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + try { + uri = FileProvider.getUriForFile(activity, "eu.siacs.conversations.files", file); + } catch (IllegalArgumentException e) { + Toast.makeText(activity,activity.getString(R.string.no_permission_to_access_x,file.getAbsolutePath()), Toast.LENGTH_SHORT).show(); + return; + } + openIntent.setDataAndType(uri, mime); + openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + } else { + uri = Uri.fromFile(file); + } openIntent.setDataAndType(uri, mime); - openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); PackageManager manager = activity.getPackageManager(); - List infos = manager.queryIntentActivities(openIntent, 0); - if (infos.size() == 0) { + List info = manager.queryIntentActivities(openIntent, 0); + if (info.size() == 0) { openIntent.setDataAndType(Uri.fromFile(file),"*/*"); } try { getContext().startActivity(openIntent); - return; } catch (ActivityNotFoundException e) { - //ignored + Toast.makeText(activity,R.string.no_application_found_to_open_file,Toast.LENGTH_SHORT).show(); } - Toast.makeText(activity,R.string.no_application_found_to_open_file,Toast.LENGTH_SHORT).show(); } public void showLocation(Message message) { diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index c03efa16..13098716 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -679,4 +679,5 @@ Me Contact asks for presence subscription Allow + No permission to access %s -- cgit v1.2.3