在micromax funbook中打开pdf文件时,文档为空(大小为0kb)时出错

时间:2012-07-30 10:19:13

标签: android

Iam尝试从服务器下载pdf并将其显示在android micromax funbook中。

我收到错误,因为文档大小为空(0 KB)。打开pdf文件时...

我是android的新手,建议我使用的编码是正确的还是不正确的下载

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

2 个答案:

答案 0 :(得分:0)

使用DownloadManager代替编写自己的下载代码。

答案 1 :(得分:0)

您应该使用c.openStream();代替c.getinputstream();