2

How can I open a .pdf file with a specific app like adobe reader using an intent? Something like:

Intent intent = getPackageManager().getLaunchIntentForPackage("com.package.name");
intent.setData(Uri.fromFile(file)); //? - set file to open
startActivity(intent);

I don't want to select an app from a list, the intent must open the file using the specified app.

langjacques
  • 407
  • 5
  • 16

2 Answers2

3

If you want to open other application then you have to give package name.

Check below code.

try {
    Intent mIntent = new Intent(Intent.ACTION_VIEW); 

    mIntent.setDataAndType(Uri.fromFile(file), "application/pdf");
    mIntent.setPackage("com.adobe.reader");
    startActivity(Intent.createChooser(mIntent, "View PDF"));
} catch (Exception e) {
    //App not found
    e.printStackTrace();
}

If you want to open anyother PDF Viewer app then just change PackageName Instead of "com.adobe.reader"

Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • Almost, the above code opens the specified app but not the file, I've tested it also with a .txt file using an app called Text Editor by Byte Mobile, same thing. Any ideas? – langjacques Jul 16 '15 at 09:20
0

You can use this answer on another thread. It is for ACTION_SEND, but you can adapt it for ACTION_VIEW in your case.

Community
  • 1
  • 1
sonic
  • 1,894
  • 1
  • 18
  • 22