0

Iam は、サーバーから pdf をダウンロードして、android micromax funbook にそのまま表示しようとしています。

ドキュメントサイズが空(0 KB)であるため、PDFファイルを開くとエラーが発生します...

私はアンドロイドを初めて使用しています。使用しているコーディングがダウンロードに適しているかどうかを教えてください

 public static void 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 len1 = 0;
                    while ((len1 = in.read(buffer)) > 0) {
                            f.write(buffer, 0, len1);
                    }
                    f.close();
            } catch (Exception e) {
                    e.printStackTrace();
            }
    }

そしてpdfを表示するためのコード。

    public void showPdf() {
            File file = new File(Environment.getExternalStorageDirectory()
                    + "/pdf/Read.pdf");
            PackageManager packageManager = getPackageManager();
            Intent testIntent = new Intent(Intent.ACTION_VIEW);
            testIntent.setType("application/pdf");
            List list = packageManager.queryIntentActivities(testIntent,
                    PackageManager.MATCH_DEFAULT_ONLY);
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            Uri uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "application/pdf");
            startActivity(intent);
        }
4

2 に答える 2

0

c.openStream();の代わりに使用する必要がありc.getinputstream();ます。

于 2012-07-30T10:49:58.620 に答える
0

独自のダウンロード コードを記述する代わりに、DownloadManagerを使用してください。

于 2012-07-30T10:28:20.837 に答える