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

Commit

Permalink
Fix #27: error when opening PDFs
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienPoupa committed Apr 18, 2020
1 parent 6de3911 commit 290b86f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">
<provider
android:name=".GenericFileProvider"
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String> list;
Expand Down Expand Up @@ -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<ResolveInfo> 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);
Expand Down Expand Up @@ -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);
}
}

This file was deleted.

0 comments on commit 290b86f

Please sign in to comment.