Skip to content Skip to sidebar Skip to footer

How To View Pdf From Assets Or Raw Folder?

I am using the MuPDF library to display pdf in my app.. It is possible to view PDFs saved in internal or external memories but the App doesn't show the pdf if they are stored in as

Solution 1:

Try below code

privatevoidreadAssetAndMakeCopy()
    {
        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);
    }

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

Make sure to include

<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />

in manifest

Post a Comment for "How To View Pdf From Assets Or Raw Folder?"