9

Does any one has an idea on how to open a PDF file in Android? My code looks this this:

public class SampleActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        CopyReadAssets();    
    }

    private void CopyReadAssets() {
        AssetManager assetManager = getAssets();

        InputStream in = null;
        OutputStream out = null;
        File file = new File(getFilesDir(), "git.pdf");
        try {
            in = assetManager.open("git.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() + "/git.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);
        }
    }
}
seaplain
  • 771
  • 7
  • 25
sai
  • 2,562
  • 8
  • 31
  • 46
  • You can look at this link: http://stackoverflow.com/questions/6491210/how-to-open-a-pdf-stored-either-in-res-raw-or-assets-folder – omi0301 Oct 15 '12 at 05:20
  • check out this http://stackoverflow.com/questions/6491210/how-to-open-a-pdf-stored-either-in-res-raw-or-assets-folder – Aamirkhan Oct 15 '12 at 05:32
  • @sai please specify what problem ur facing, i.e. any exception/error, or app displays list of available pdf Viewer list? – Chanchal Shelar Oct 15 '12 at 06:08
  • 1
    You might wish to log the `Uri` you are trying to use, as I suspect it is not the value that you think it is. – CommonsWare Oct 15 '12 at 10:44
  • Duplicate: http://stackoverflow.com/questions/17085574/read-a-pdf-file-from-assets-folder – csvan Apr 15 '14 at 08:46

1 Answers1

2

Here is the code for opening pdf file from asset folder, but you must have pdf reader installed on your device :

    private void CopyAssets() {

        AssetManager assetManager = getAssets();

        InputStream in = null;
        OutputStream out = null;
        File file = new File(getFilesDir(), "fileName.pdf");
        try {
            in = assetManager.open("fileName.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() + "/fileName.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);
        }
    }
Sargam
  • 987
  • 1
  • 8
  • 6
  • 5
    This answer is essentially copied verbatim from this one: http://stackoverflow.com/questions/17085574/read-a-pdf-file-from-assets-folder – csvan Apr 15 '14 at 08:46