Created
July 19, 2018 03:41
-
-
Save hilfritz/7c90bc11cda09a8c8783f42fbde390cb to your computer and use it in GitHub Desktop.
Android: open pdf using external app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static void openPDFFiile(Context context,File file){ | |
| //OPEN THE PDF | |
| String mimeTypeStr = "application/pdf"; | |
| Intent intent2 = new Intent(Intent.ACTION_VIEW); | |
| //intent2.setDataAndType(uri, mimeTypeStr); | |
| //Uri apkURI = FileProvider.getUriForFile( | |
| // context, | |
| // context.getApplicationContext() | |
| // .getPackageName() + ".provider", file); | |
| //BuildConfig.APPLICATION_ID | |
| Uri apkURI = FileProvider.getUriForFile( | |
| context, | |
| context.getApplicationContext() | |
| .getPackageName() + ".util.GenericFileProvider", file); | |
| //Uri photoURI = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".my.package.name.provider", file); | |
| intent2.setDataAndType(apkURI, mimeTypeStr); | |
| intent2.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); | |
| intent2.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); | |
| Intent intent1 = Intent.createChooser(intent2, "Open With"); | |
| try { | |
| context.startActivity(intent1); | |
| } catch (ActivityNotFoundException e) { | |
| e.printStackTrace(); | |
| Toast.makeText(context, "Please install pdf viewer app.", Toast.LENGTH_SHORT).show(); | |
| // Instruct the user to install a PDF reader here, or something | |
| }catch (Exception e){ | |
| e.printStackTrace(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment