使用PDF查看器库中的Andr​​oid应用程序从网址阅读PDF [英] reading pdf from url using pdfViewer library in android app

查看:127
本文介绍了使用PDF查看器库中的Andr​​oid应用程序从网址阅读PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个Android应用程序在我的code.Firstly下载的应用程序从网页到外置SD卡中的文件,然后整合PDF查看器库中查看PDF文件从获取URL从那里应用程序是越来越有PDF查看器打开library.It工作正常,如果文件尺寸很小,但​​如果PDF文件包含图像和尺寸更在SD卡显示的下载文件大小为0KB。
有人可以帮我为什么会是这样?

以下是java的code:

 公共类MainActivity延伸活动{
    静态上下文的applicationContext;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        的applicationContext = getApplicationContext();        。字符串extStorageDirectory = Environment.getExternalStorageDirectory()的toString();
        文件夹=新的文件(extStorageDirectorypdfDownloads);
        folder.mkdir();
        档案文件=新的文件(文件夹,android.pdf);
        尝试{
            如果(!file.exists()){
                file.createNewFile();
            }
        }赶上(IOException异常E1){
            e1.printStackTrace();
        }
        布尔downloadFile = downloadFile(http://www.irs.gov/pub/irs-pdf/fw4.pdf文件);
        如果(文件=空&放大器;!&放大器; file.exists()及&放大器; file.length()大于0){
            意向意图=新意图(这一点,com.example.soniapdf.Second.class);
            intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME,
                    file.getAbsolutePath());
            startActivity(意向);
        }
    }    公共静态布尔downloadFile(字符串fileUrl,文件目录){
        尝试{
            FileOutputStream中F =新的FileOutputStream(目录);
            URL U =新的URL(fileUrl);
            HttpURLConnection的C =(HttpURLConnection类)u.openConnection();
            c.setRequestMethod(GET);
            c.setDoOutput(真);
            c.connect();
            在的InputStream = c.getInputStream();            字节[]缓冲区=新的字节[1024];
            INT LEN = 0;            //
             INT文件长度= c.getContentLength();
             总长= 0;
            //
                    Toast.makeText(ApplicationContext一下载PDF格式...,2000).show();
            而((LEN = in.read(缓冲液))大于0){
                总+ = LEN;
            //Toast.makeText(applicationContext,下载PDF:剩下的+(文件长度/总)+%,1).show();
                f.write(缓冲液,0,LEN);
            }
            f.close();
            返回true;
        }赶上(例外五){
            e.printStackTrace();
            返回false;
        }    }
}


解决方案

这是用于显示Android应用PDF正在使用从的 http://docs.google.com/viewer

 字符串DOC =< IFRAME SRC =HTTP://docs.google.com/viewer URL = +位置PDF文件+'
          WIDTH =100%HEIGHT =100%
          风格=边界:无;'>< / IFRAME>中;

一个样本如下所示

 字符串DOC =< IFRAME src='http://docs.google.com/viewer?url=http://www.iasted.org/conferences/formatting/$p$psentations-tips.ppt&embedded=true'
          WIDTH =100%HEIGHT =100%
          风格=边界:无;'>< / IFRAME>中;

code

 的WebView WV =(的WebView)findViewById(R.id.webView);
wv.getSettings()setJavaScriptEnabled(真)。
wv.getSettings()setPluginsEnabled(真)。
。wv.getSettings()setAllowFileAccess(真);
wv.loadUrl(DOC);
//wv.loadData(DOC,text / html的,UTF-8);

和清单中提供

 <使用许可权的android:NAME =android.permission.INTERNET对/>

看到这个答案

修改

如果您的PDF文档在线访问,使用谷歌文档查看器要在WebView中打开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的条款和的可能不适合商用使用

以下是适合于商用使用SDK列表

I made an android app for viewing Pdf file fetched from URL by integrating pdfViewer library in my code.Firstly app downloading the file from web to external sd card then from there the app is getting opened with PdfViewer library.It is working fine if the file size is small but if the pdf file contains images and size is more , the downloaded file size shown in sdcard is 0kb. Can someone help me out why this is so?

Following is java code :

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;
        }

    }
}

解决方案

This is a way for showing PDF in android app that is embedding the PDF document to android webview using support from http://docs.google.com/viewer

pseudo

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

a sample is is shown below

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>";

Code

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");

and in manifest provide

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

SEE THIS ANSWER

EDIT

If your PDF document is accessible online, use the Google Docs Viewer to open your PDF in a WebView

REFER

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

Don't Know how Stable These are

Here is the list of the other open sources PDF readers running on the top of the Android

Please note that these and any other project derived from MuPDF is bound by the terms of GPL and may not be suitable for the commerical use.

The following is a list of SDKs suitable for commerical use:

这篇关于使用PDF查看器库中的Andr​​oid应用程序从网址阅读PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆