Skip to content Skip to sidebar Skip to footer

Open A Pdf File Programmatically

I am working on pdf. I am trying to open a pdf file from my application using the code below. But I failed to open. private void openPdf() { File file = new File('mnt/sdca

Solution 1:

I got solution of above problem, so try once;

Steps:-

  1. create assets folder in src under your app name.

  2. In this assets folder keep your pdf files e.g. schedule1.pdf.

  3. now come your activity i.e MainActivity.java

  4. setListener on any UI component what you want i.e (Button, ImageView, ImageButton);

  5. In this listener call one user defined method i.e. openPDFFiles()

the openPDFFiles() method have below code:

privatevoidopenPDFFiles() {
    AssetManager assetManager = getAssets();

    InputStream in = null;
    OutputStream out = null;
    File file = new File(getFilesDir(), “schedule1.pdf”);//here schedule1.pdf is the pdf file name which is keep in assets folder.try {
        in = assetManager.open(“schedule1.pdf”);
        out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

        copyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e) {
        Log.e(“tag”, e.getMessage());
    }

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.parse(“file://” + getFilesDir() + “/schedule1.pdf”), “application/pdf”);

    startActivity(intent);
}

privatevoidcopyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = newbyte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

Solution 2:

Try LuxuryMode's method: https://stackoverflow.com/a/8221594/1500067

I think your just missing the adobe package intent.setPackage("com.adobe.reader");

Solution 3:

for android 9 and 10 you should use this code

1-create one class

publicclassGenericFileProviderextendsFileProvider {

}

2-in res directory create one directory to name of xml and create one file to name of provider_paths.xml and add this code

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

3-in manifests in application add this code

<providerandroid:name=".Model.Utilitys.GenericFileProvider"android:authorities="${applicationId}.provider"android:exported="false"android:grantUriPermissions="true"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/provider_paths"/></provider>

4-you should get Permission Manifest.permission.READ_EXTERNAL_STORAGE

5-and add this codes

String fileName="yourfile.pdf";

Filefile=newFile(Environment.getExternalStorageDirectory()+"/Android/data/ir.tdaapp.paymanyar/files/File",fileName);

Stringextension= MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());

StringmimeType= MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);

Intentintent=newIntent(Intent.ACTION_VIEW);

intent.setFlags(FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_NEW_TASK);

Uriuri= FileProvider.getUriForFile(getContext(), getActivity().getApplicationContext().getPackageName() + ".provider", file);

intent.setDataAndType(uri, mimeType);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

startActivity(Intent.createChooser(intent, "choseFile"));

6-I suggest you add this code to the application in manifests

android:requestLegacyExternalStorage="true"

Solution 4:

I have nearly identical code that works fine, though I'm not opening a file from SD card in my app.

ActivitymActivity=/* your activity */...;
StringmFileName=/* path of my PDF file */...;

Uriuri= Uri.fromFile(mActivity.getFileStreamPath(mFileName));

try
{
    IntentintentUrl=newIntent(Intent.ACTION_VIEW);
    intentUrl.setDataAndType(uri, "application/pdf");
    intentUrl.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    mActivity.startActivity(intentUrl);
}
catch (ActivityNotFoundException e)
{
    Toast.makeText(mActivity, "No PDF Viewer Installed", Toast.LENGTH_LONG).show();
}

so your approach is right on. Make sure you can open the file first ... i.e. use mActivity.openFileInput() to ensure you have a readable PDF.

Solution 5:

Post a Comment for "Open A Pdf File Programmatically"