Faraz
Faraz

Reputation: 643

How to open a PDF file from in-app browser?

I have a basic File Browser inside my app and I want it to be able to open PDF files, but for some reason, even though I can tap on the PDF files and they open for a second, they go away and my phone gives a Toast message with an error, and this Toast I haven't programmed anywhere in my code. This is weird because the same PDF file opens from my other activities without any issues. (See the OpenPDF() Method which works from my PDFManager class) I researched a lot about how to fix this but couldn't find a clear answer. Thanks for helping!

The toast error: Cannot display PDF (filename.pdf cannot be opened)

OnListItemClick function from AndroidFileBrowser:

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        String filename = (String) getListAdapter().getItem(position);
        if (path.endsWith(File.separator)) {
            filename = path + filename;
        } else {
            filename = path + File.separator + filename;
        }
        pdf = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/PATH/" + filename);
        Uri path = Uri.fromFile(pdf);
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(path, "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
        e.printStackTrace();
        }
    }

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.faraz.pdfreader"
          xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".SimpleScannerActivity"
            android:label="@string/simple_scanner_activity"
            android:theme="@style/AppOverlayTheme">
        </activity>
        <activity android:name=".PDFManager"/>
        <activity android:name=".AndroidFileBrowser"/>
                  <!--android:screenOrientation="portrait"-->
    </application>
</manifest>

OpenPDF() method from my PDFManager class

    public void openPDF() {
        File pdf = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/PATH/" + pdfname + ".pdf");
        if (pdf.exists()) {
            Uri path = Uri.fromFile(pdf);
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setDataAndType(path, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            try {
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(PDFManager.this,
                        "No application available to view PDF!",
                        Toast.LENGTH_SHORT).show();
     /*                Intent intent = new Intent(Intent.ACTION_VIEW)
                        .setData(Uri.parse("http://docs.google.com/gview?embedded=true&url=" + url));
                startActivity(intent);
            }
     */
            }
        }
    }
 }

Upvotes: 0

Views: 661

Answers (1)

TheTool
TheTool

Reputation: 309

In your OnItemClick():

if (path.endsWith(File.separator)) 
{
    filename = path + filename;
} 
else 
{
    filename = path + File.separator + filename;
}

pdf = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/PATH/" + filename);
Uri path = Uri.fromFile(pdf);

You use "path" before you initialize it, so depending on your intentions either change the name of one or the other, or declare it before your if-statement not after.

Upvotes: 1

Related Questions