Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open a pdf file programmatically

Tags:

android

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/sdcard.test.pdf");
        Uri path = Uri.fromFile(file);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(path);
        intent.setType("application/pdf");
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(getActivity(), "No application found",
                    Toast.LENGTH_SHORT).show();
        }
    }

When I tried this code in emulator,it shows a toast saying "No application found"(bcoz,normally no pdf viewing application are installed in emulator). When I tested the same thing in device (specifically in funbook tab and in sony tab), it neither showed the Toast message nor it opened the pdf file. Can anybody point out the mistake in my code. Actually I am working with pdf for the first time. So my question is,

  1. In device it didn't showed the toast message, which means there is a pdf viewing application installed in my phone? is it right?
  2. If so, why the pdf is not opened using third party application.
  3. If I want to list all pdf viewing applications installed in my phone to the user, what changes should I make in this code?
like image 989
Sanal Varghese Avatar asked Dec 08 '22 23:12

Sanal Varghese


1 Answers

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:

private void openPDFFiles() {
    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);
}

private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}
like image 169
sachin pangare Avatar answered Jan 03 '23 05:01

sachin pangare