简体   繁体   中英

Open pdf extern from android app without acrobat reader

I want to open a pdf from my android app in an external viewer. When I have installed Acrobat Reader everything works great. But when Acrobar reader isn't installed he can't find a viewer. But another app on the same device is able to open pdfs in google drive. I want this behaviour, too. What I have to do, that this works for my app?

So I open pdfs at the moment:

        final Intent intent = new Intent(Intent.ACTION_VIEW);
        String extension = fileName.substring(fileName.lastIndexOf('.'));
        intent.setDataAndType(Uri.fromFile(new File("fileName")), "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (!this.context.getPackageManager()
                .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).isEmpty()) {
            startActivity(intent);
            finish();
        }

Try using Intent.createChooser() : http://developer.android.com/training/basics/intents/sending.html#AppChooser

Intent intent=...//your intent
Intent chooser = Intent.createChooser(intent, "Open PDF");
startActivity(chooser);

EDIT:

For google drive the intent action is Intent.ACTION_SEND, and you need to set extras make your intent with:

Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + your_file_path));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM