open a pdf on android with an application already installed

1

I have the following code but it does not recognize the file

String dir = Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DOWNLOADS + "/"+nombPdf;//Directory()+"/2555524_01012018_2390_RTF.pdf";//
    Toast.makeText(context, dir, Toast.LENGTH_LONG).show();
    File arch = new File(dir);
    if (arch.exists()) {
        Uri uri = Uri.parse(String.valueOf(arch));
        Intent intent = new Intent(Intent.ACTION_VIEW );
        intent.setData(uri);
        intent.setType( "application/pdf");
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setPackage("com.adobe.reader");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            Intent chooser = null;
            chooser = Intent.createChooser(intent, "Abrir factura");
            startActivity(intent);
            //startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(context, "No existe una aplicación para abrir el PDF", Toast.LENGTH_SHORT).show();
        }
    }

and the following permissions on the manifest          

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".MainActivity">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

    
asked by sergio gomez 18.04.2018 в 11:24
source

1 answer

2

I already found the problem was because of the need to use file provider in the new versions of android this would be the code that should go in the manifest file

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" />
        </provider>

this would be the code to put in the xml that contains the routes of the files

<?xml version="1.0" encoding="utf-8"?>

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="." />
</paths>

and this one of the function that opens the files

            String s= String.valueOf(file);
            File arch = new File(s);
            if (arch.exists()) {
                Uri uri = FileProvider.getUriForFile(getContext(), getActivity().getApplicationContext().getPackageName() + ".provider", arch);
                Intent intent = new Intent(Intent.ACTION_VIEW );
                intent.setDataAndType(uri, "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                try {
                    startActivity(intent);
                } catch (ActivityNotFoundException e) {
                    Utils.showSnackBar(root.getResources().getString(R.string.error_pdf), root);
                }
    
answered by 19.04.2018 / 09:40
source