1

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 assets folder of the app... How to view in app PDFs?

I've seen solutions which say thatbwe can copy our in app PDFs in app associated folder and then use them later on... but I can't get that..

here's the code -

public class MainActivity extends Activity {

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

        Button showPDFBtn = (Button)findViewById(R.id.btn_show_pdf);
        showPDFBtn.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {

        Uri uri = Uri.parse("file:///android_asset/test.pdf");
        Intent intent = new Intent(MainActivity.this, MuPDFActivity.class);
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(uri);
        startActivity(intent);



}}
  );
  }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
}
Darshan
  • 4,020
  • 2
  • 18
  • 49
  • 1
    that 's true that you can copy file into an internal storage, and then open it. Don't you know how to do that? – Vladyslav Matviienko Nov 28 '16 at 06:21
  • 1
    refer http://stackoverflow.com/questions/6491210/how-to-open-a-pdf-stored-either-in-res-raw-or-assets-folder – sasikumar Nov 28 '16 at 06:22
  • @VladMatvienko No, I am new to android so I couldn't grt that... can u give a code example? – Darshan Nov 28 '16 at 06:25
  • @sasikumar I've seen it.. I could understand that how they saved the pdf in a folder but how to read it? – Darshan Nov 28 '16 at 06:26
  • so your question really is `how to copy file from assets to internal storage`, right? Why don't you ask what you really want? I really can't give you example in this question because of 2 reasons: 1. My answer won't match your question. 2. There are already hundreds of answers for your question on StackOverflow – Vladyslav Matviienko Nov 28 '16 at 06:27
  • i meant to Copy the pdf in the assets folder and then view it in the application because muPDF cant read PDFs in assets.. :) – Darshan Nov 28 '16 at 06:28

1 Answers1

5

Try below code

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

Make sure to include

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

in manifest

Surya Prakash Kushawah
  • 3,185
  • 1
  • 22
  • 42