Skip to content
This repository was archived by the owner on Aug 31, 2022. It is now read-only.

Commit 290b86f

Browse files
committed
Fix #27: error when opening PDFs
1 parent 6de3911 commit 290b86f

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
android:supportsRtl="true"
1111
android:theme="@style/AppTheme">
1212
<provider
13-
android:name=".GenericFileProvider"
13+
android:name="androidx.core.content.FileProvider"
1414
android:authorities="${applicationId}.provider"
1515
android:exported="false"
1616
android:grantUriPermissions="true">

app/src/main/java/com/poupa/attestationdeplacement/AttestationAdapter.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,13 @@
1111
import android.graphics.drawable.Drawable;
1212
import android.net.Uri;
1313
import android.os.Build;
14-
import android.util.DisplayMetrics;
1514
import android.view.LayoutInflater;
1615
import android.view.View;
1716
import android.view.ViewGroup;
18-
import android.view.Window;
19-
import android.view.WindowManager;
2017
import android.widget.BaseAdapter;
2118
import android.widget.ImageButton;
2219
import android.widget.ImageView;
2320
import android.widget.ListAdapter;
24-
import android.widget.RelativeLayout;
2521
import android.widget.TextView;
2622
import android.widget.Toast;
2723

@@ -31,6 +27,7 @@
3127

3228
import java.io.File;
3329
import java.util.ArrayList;
30+
import java.util.List;
3431

3532
public class AttestationAdapter extends BaseAdapter implements ListAdapter {
3633
private ArrayList<String> list;
@@ -139,19 +136,22 @@ public void onClick(View v) {
139136
// Clicking on items
140137
String fileName = getItem(position) + ".pdf";
141138

142-
PackageManager pm = context.getPackageManager();
143-
144139
Intent intent = new Intent(Intent.ACTION_VIEW);
145140

146-
intent.setDataAndType(AttestationAdapter.this.getUri(fileName), "application/pdf");
141+
Uri uri = getUri(fileName);
142+
143+
intent.setDataAndType(uri, "application/pdf");
147144

148-
ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
145+
List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
149146

150-
if (info != null) {
151-
intent = new Intent(Intent.ACTION_VIEW);
152-
intent.setDataAndType(AttestationAdapter.this.getUri(fileName), "application/pdf");
153-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
154-
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
147+
if (resInfoList.size() > 0) {
148+
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
149+
// https://stackoverflow.com/a/32950381/11989865
150+
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
151+
for (ResolveInfo resolveInfo : resInfoList) {
152+
String packageName = resolveInfo.activityInfo.packageName;
153+
context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
154+
}
155155
}
156156
intent = Intent.createChooser(intent, "Open File");
157157
context.startActivity(intent);
@@ -181,14 +181,13 @@ public void onClick(View v) {
181181
return convertView;
182182
}
183183

184+
/**
185+
* Get the file Uri
186+
* @param fileName
187+
* @return
188+
*/
184189
private Uri getUri(String fileName) {
185-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
186-
File file = new File(context.getFilesDir(), fileName);
187-
return FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
188-
}
189-
190-
String filePath = context.getFilesDir() + "/" + fileName;
191-
192-
return Uri.parse(filePath);
190+
File file = new File(context.getFilesDir(), fileName);
191+
return FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
193192
}
194193
}

app/src/main/java/com/poupa/attestationdeplacement/GenericFileProvider.java

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)