2

コードにpdfViewerライブラリを統合することにより、URLから取得したPdfファイルを表示するためのAndroidアプリを作成しました.まず、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;
        }

    }
}
4

1 に答える 1

2

これは、 http: //docs.google.com/viewer のサポートを使用して Android WebView に PDF ドキュメントを埋め込んでいる Android アプリで PDF を表示する方法です。

擬似

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

この回答を見る

編集

PDF ドキュメントにオンラインでアクセスできる場合は、Google Docs Viewerを使用して PDF を WebView で開きます。

参照

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

これらがどれほど安定しているかわかりません

これは、Android 上で実行されている他のオープン ソース PDF リーダーのリストです。

MuPDF から派生したこれらのプロジェクトおよびその他のプロジェクトは、GPL の条件に拘束され、商用利用には適していない可能性があることに注意してください。

以下は、商用利用に適した SDK のリストです。

于 2013-02-19T09:36:13.417 に答える