Tiago Paza
Tiago Paza

Reputation: 43

Use Android Intent to open PDF in Flutter

I'm using the package android_intent and never work in Android native, but searching in official documentation, I create this code based in Kotlin codes.

            String uriFile =
                Uri.encodeFull("content://${contentFile.path}");

            if (Platform.isAndroid) {
              final AndroidIntent intent = AndroidIntent(
                action: "action_view",
                data: uriFile,
                type: "application/pdf",
                // type: "multipart/",
                flags: [
                  Flag.FLAG_GRANT_READ_URI_PERMISSION,
                  Flag.FLAG_GRANT_PERSISTABLE_URI_PERMISSION,
                  Flag.FLAG_ACTIVITY_NEW_TASK
                ],
              );
              intent.launch();

This example generate this log:

V/IntentSender(25283): Sending intent Intent { act=android.intent.action.VIEW dat=content:///data/user/0/br.com.encaixaonline/app_flutter/encaixa-online/arquivos/boleto_facil_exemplo.pdf typ=application/pdf flg=0x10000041 (has extras) }

It generates the alert and displays the applications that allow you to open the PDF, but it does not display the file in any of the applications.

Upvotes: 3

Views: 3635

Answers (1)

Allan Wolski
Allan Wolski

Reputation: 59

You can use the open_file package to open PDF files in Android and iOS.

import 'package:open_file/open_file.dart';

OpenFile.open('/sdcard/sample.pdf');

If you want to render and show a PDF file within your app, you can use the native_pdf_view.

import 'package:native_pdf_view/native_pdf_view.dart';

final pdfController = PdfController(
  document: PdfDocument.openAsset('assets/sample.pdf'),
);

Widget pdfView() => PdfView(
  controller: pdfController,
);

Upvotes: 2

Related Questions