Skip to content

Instantly share code, notes, and snippets.

@hilfritz
Created July 19, 2018 03:41
Show Gist options
  • Select an option

  • Save hilfritz/7c90bc11cda09a8c8783f42fbde390cb to your computer and use it in GitHub Desktop.

Select an option

Save hilfritz/7c90bc11cda09a8c8783f42fbde390cb to your computer and use it in GitHub Desktop.
Android: open pdf using external app
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