Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 325
Tiago Paza Avatar asked Mar 01 '26 18:03

Tiago Paza


1 Answers

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,
);
like image 163
Allan Wolski Avatar answered Mar 07 '26 09:03

Allan Wolski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!