【问题标题】:reading pdf from url using pdfViewer library in android app在 android 应用程序中使用 pdfViewer 库从 url 读取 pdf
【发布时间】:2013-02-03 21:43:08
【问题描述】:

我制作了一个 android 应用程序,通过在我的代码中集成 pdfViewer 库来查看从 URL 获取的 Pdf 文件。首先应用程序将文件从 Web 下载到外部 sd 卡,然后从那里使用 PdfViewer 库打开应用程序。它工作正常如果文件大小很小,但如果 pdf 文件包含图像并且大小更多,则 sdcard 中显示的下载文件大小为 0kb。 有人可以帮我看看为什么会这样吗?

以下是java代码:

public class MainActivity extends Activity {
    static Context applicationContext;

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

        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
        File folder = new File(extStorageDirectory, "pdfDownloads");
        folder.mkdir();
        File file = new File(folder, "android.pdf");
        try {
            if(!file.exists()) {
                file.createNewFile();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        boolean downloadFile = downloadFile("http://www.irs.gov/pub/irs-pdf/fw4.pdf", file);
        if (file!=null && file.exists() && file.length() > 0){
            Intent intent = new Intent(this, com.example.soniapdf.Second.class);
            intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME,
                    file.getAbsolutePath());
            startActivity(intent);
        }
    }

    public static boolean downloadFile(String fileUrl, File directory) {
        try {
            FileOutputStream f = new FileOutputStream(directory);
            URL u = new URL(fileUrl);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();
            InputStream in = c.getInputStream();

            byte[] buffer = new byte[1024];
            int len = 0;

            //
             int fileLength = c.getContentLength();
             long total = 0;
            //
                    Toast.makeText(applicationContext, "Downloading PDF...", 2000).show();


            while ((len = in.read(buffer)) > 0) {
                total += len;


            //Toast.makeText(applicationContext, "Downloading PDF: remaining " + (fileLength / total  )+ "%", 1).show();
                f.write(buffer, 0, len);
            }
            f.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }
}

【问题讨论】:

  • 有什么异常吗?
  • 您正在从服务器获取成功之前创建文件...您应该首先进行http连接检查响应代码..如果响应代码是200..然后打开FileOutputStream并写入数据...使用您当前的代码如果在建立网络连接时出现任何错误,将创建一个空文件...
  • @PrafulBhatnagar 你能帮我写代码吗?如何让http连接检查响应码?

标签: android


【解决方案1】:

这是一种在 android 应用程序中显示 PDF 的方法,该应用程序使用 http://docs.google.com/viewer 的支持将 PDF 文档嵌入到 android webview 中

String doc="<iframe src='http://docs.google.com/viewer?url=+location to your PDF File+' 
          width='100%' height='100%' 
          style='border: none;'></iframe>";

示例如下所示

String doc="<iframe src='http://docs.google.com/viewer?url=http://www.iasted.org/conferences/formatting/presentations-tips.ppt&embedded=true' 
          width='100%' height='100%' 
          style='border: none;'></iframe>";

代码

WebView  wv = (WebView)findViewById(R.id.webView); 
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginsEnabled(true);
wv.getSettings().setAllowFileAccess(true);
wv.loadUrl(doc);
//wv.loadData( doc, "text/html",  "UTF-8");

并在清单中提供

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

SEE THIS ANSWER

编辑

如果您的 PDF 文档可以在线访问,请使用 Google Docs Viewer 在 Web 视图中打开您的 PDF

REFER

 wv.loadUrl("https://docs.google.com/gview?embedded=true&url=http://www.irs.gov/pub/irs-pdf/fw4.pdf");

不知道这些有多稳定

这是在 Android 上运行的其他开源 PDF 阅读器的列表

请注意,这些以及从 MuPDF 派生的任何其他项目均受 GPL 和may not be suitable for the commerical use 条款的约束。

以下是适合商业使用的SDK列表:

【讨论】:

  • 你能再把自定义库链接发给我吗?
  • 我使用了上面的代码。它工作正常,我可以显示pdf文件。但我需要从这个文件中复制文本。请帮助我如何实现这一目标。
  • 我不知道如何回答主要问题,如何使用 pdfViewer 来回答?我认为他不是在问如何使用 webview 来实现
猜你喜欢
  • 2011-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多