From 290b86f7f58353160b20135cbdb0bbe7dfe3ef2f Mon Sep 17 00:00:00 2001 From: adrienpoupa Date: Sat, 18 Apr 2020 16:21:55 -0400 Subject: [PATCH] Fix #27: error when opening PDFs --- app/src/main/AndroidManifest.xml | 2 +- .../AttestationAdapter.java | 41 +++++++++---------- .../GenericFileProvider.java | 6 --- 3 files changed, 21 insertions(+), 28 deletions(-) delete mode 100644 app/src/main/java/com/poupa/attestationdeplacement/GenericFileProvider.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 08de490..2897c85 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -10,7 +10,7 @@ android:supportsRtl="true" android:theme="@style/AppTheme"> diff --git a/app/src/main/java/com/poupa/attestationdeplacement/AttestationAdapter.java b/app/src/main/java/com/poupa/attestationdeplacement/AttestationAdapter.java index 125af4a..401408d 100644 --- a/app/src/main/java/com/poupa/attestationdeplacement/AttestationAdapter.java +++ b/app/src/main/java/com/poupa/attestationdeplacement/AttestationAdapter.java @@ -11,17 +11,13 @@ import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build; -import android.util.DisplayMetrics; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.view.Window; -import android.view.WindowManager; import android.widget.BaseAdapter; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.ListAdapter; -import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; @@ -31,6 +27,7 @@ import java.io.File; import java.util.ArrayList; +import java.util.List; public class AttestationAdapter extends BaseAdapter implements ListAdapter { private ArrayList list; @@ -139,19 +136,22 @@ public void onClick(View v) { // Clicking on items String fileName = getItem(position) + ".pdf"; - PackageManager pm = context.getPackageManager(); - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setDataAndType(AttestationAdapter.this.getUri(fileName), "application/pdf"); + Uri uri = getUri(fileName); + + intent.setDataAndType(uri, "application/pdf"); - ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); + List resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); - if (info != null) { - intent = new Intent(Intent.ACTION_VIEW); - intent.setDataAndType(AttestationAdapter.this.getUri(fileName), "application/pdf"); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + if (resInfoList.size() > 0) { + intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + // https://stackoverflow.com/a/32950381/11989865 + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { + for (ResolveInfo resolveInfo : resInfoList) { + String packageName = resolveInfo.activityInfo.packageName; + context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION); + } } intent = Intent.createChooser(intent, "Open File"); context.startActivity(intent); @@ -181,14 +181,13 @@ public void onClick(View v) { return convertView; } + /** + * Get the file Uri + * @param fileName + * @return + */ private Uri getUri(String fileName) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - File file = new File(context.getFilesDir(), fileName); - return FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file); - } - - String filePath = context.getFilesDir() + "/" + fileName; - - return Uri.parse(filePath); + File file = new File(context.getFilesDir(), fileName); + return FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file); } } \ No newline at end of file diff --git a/app/src/main/java/com/poupa/attestationdeplacement/GenericFileProvider.java b/app/src/main/java/com/poupa/attestationdeplacement/GenericFileProvider.java deleted file mode 100644 index f894ba1..0000000 --- a/app/src/main/java/com/poupa/attestationdeplacement/GenericFileProvider.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.poupa.attestationdeplacement; - -import androidx.core.content.FileProvider; - -public class GenericFileProvider extends FileProvider { -}