1

I am developing an app in that I wanted to display a pdf file from asset. I did so much google and also tried number of permutations and combinations but not working.

CODE:

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

    InputStream in = null;
    OutputStream out = null;
    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/abc.pdf");
    try
    {
        in = assetManager.open("abc.pdf");
        out = getActivity().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.fromFile(file), "application/pdf");
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    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);
    }
}

when I click on list, I call CopyReadAssets() function then it prompts me in which viewer you want to open then I click on AdobeReader then it shows following error.

enter image description here

Mitesh Shah
  • 1,729
  • 2
  • 19
  • 37

3 Answers3

5

I have check your code There are some mistake that you have to change and May be you have copied code from here.

Replace AssetManager assetManager = getAssets();

Instead of AssetManager assetManager = getActivity().getAssets();

Direct Use File file = new File(getFilesDir(), "abc.pdf");

Instead of

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/abc.pdf");

Hope its works for you.!!!

Community
  • 1
  • 1
Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
1

You can do this By using web view-

     webview = (WebView) findViewById(R.id.prayertimes_webview);
     webview.getSettings().setJavaScriptEnabled(true);
     webview.getSettings().setPluginsEnabled(true);
     webview.getSettings().setSupportZoom(true);
     webview.getSettings().setBuiltInZoomControls(true);
            String urll = "http://docs.google.com/gview?embedded=true&url=" + url;
            webview.setWebViewClient(new WebViewClient()); 
            webview.loadUrl(urll);
Shani Goriwal
  • 2,111
  • 1
  • 18
  • 30
  • you can open you pdf by sending it to google as docs.google.com/gview?embedded=true&url= and passing you pdf file. – Shani Goriwal Jan 30 '14 at 09:42
  • @Lucky If android provides a native features then why & who will try to load pdf from remote server.(web view is a browser and that will take a load to open a PDF that is bad approach) – Chintan Khetiya Jan 30 '14 at 10:18
  • @chintankhetiya You can achive your task using this if you r not getting by above code,that was alternate solution not the primary. – Shani Goriwal Jan 30 '14 at 10:50
0

The PDF file cannot be seen from the PDF reader because anything inside of your assets are private to your app. You have to make it public in some way. There are several ways for this.

The most sophisticated way is to implement a public ContentProvider and override the openAssetFile method in it. Pass the URL for the file through Intent, and the PDF reader should be able to use ContentResolver and get the PDF file by openAssetFileDescriptor method.

Here's a link. - http://www.nowherenearithaca.com/2012/03/too-easy-using-contentprovider-to-send.html

Yuichi Araki
  • 3,438
  • 1
  • 19
  • 24