Browse Source

Fixed checking attachment viewer

main
M66B 6 years ago
parent
commit
eba14cbf84
1 changed files with 18 additions and 21 deletions
  1. +18
    -21
      app/src/main/java/eu/faircode/email/AdapterAttachment.java

+ 18
- 21
app/src/main/java/eu/faircode/email/AdapterAttachment.java View File

@ -99,15 +99,26 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
}); });
} else { } else {
// Build file name // Build file name
File dir = new File(context.getCacheDir(), "attachments");
dir.mkdir();
final File dir = new File(context.getCacheDir(), "attachments");
final File file = new File(dir, TextUtils.isEmpty(attachment.name) ? "noname" : attachment.name); final File file = new File(dir, TextUtils.isEmpty(attachment.name) ? "noname" : attachment.name);
// https://developer.android.com/reference/android/support/v4/content/FileProvider
Uri uri = FileProvider.getUriForFile(context, "eu.faircode.email", file);
// Build intent
final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
//intent.setType(attachment.type);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Log.i(Helper.TAG, "Sharing " + file + " type=" + attachment.type);
// Set permissions
List<ResolveInfo> targets = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : targets)
context.grantUriPermission(resolveInfo.activityInfo.packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
// Check if viewer available // Check if viewer available
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.fromFile(file));
PackageManager pm = context.getPackageManager();
if (pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) == null) {
if (targets.size() == 0) {
Toast.makeText(context, R.string.title_no_viewer, Toast.LENGTH_LONG).show(); Toast.makeText(context, R.string.title_no_viewer, Toast.LENGTH_LONG).show();
return; return;
} }
@ -119,6 +130,7 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
try { try {
// Create file // Create file
if (!file.exists()) { if (!file.exists()) {
dir.mkdir();
file.createNewFile(); file.createNewFile();
FileOutputStream fos = null; FileOutputStream fos = null;
@ -131,21 +143,6 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
} }
} }
// https://developer.android.com/reference/android/support/v4/content/FileProvider
Uri uri = FileProvider.getUriForFile(context, "eu.faircode.email", file);
// Build intent
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
//intent.setType(attachment.type);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Log.i(Helper.TAG, "Sharing " + file + " type=" + attachment.type);
// Set permissions
List<ResolveInfo> targets = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : targets)
context.grantUriPermission(resolveInfo.activityInfo.packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
// Start viewer // Start viewer
context.startActivity(intent); context.startActivity(intent);
} catch (Throwable ex) { } catch (Throwable ex) {


Loading…
Cancel
Save