1

私はダウンロード用にこのコードを書きました。pdfファイルurlurlこれです-

 String fileURL= "http://www.vivekananda.net/PDFBooks/History_of_India.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.getResponseCode();
        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();
    }

}

しかし、これは応答コード 405 のファイルが見つからない例外を示しています。なぜこれが起こったのかわかりません。助けてください..!!

これは、SDカードにファイルを作成した私のコードです-

これをコーディング

  public void createPdfFile(){
        String extStorageDirectory = Environment.getExternalStorageDirectory()
                .toString();
                File folder = new File(extStorageDirectory, "pdf");
                folder.mkdir();
                 file = new File(folder, "storrage_data.pdf");
                try {
                    file.createNewFile();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }        
    }`

この後、onResume(); からこのようにスレッドでダウンロード メソッドを呼び出しています。onCreateからのbeacuseは、「Network On Main Thread」というエラーを表示します。どこが間違っているのか、わかりません:(

これをコーディング

 public void downloadFile(){        

     new Thread(new Runnable() {

            @Override
            public void run() {             
         Downloader.DownloadFile(url, file);
          showPdf();

            }
        }).start();   


    }
4

4 に答える 4

2

考えられる理由は、目的のフォルダーが存在しないことです。まず、存在するかどうかを確認します。ない場合は作成します。次に、fileoutputstream を作成し、それに書き込みます。

于 2015-12-30T06:02:17.357 に答える
1

DownloadManager を使用することをお勧めします。ダウンロード中に発生する可能性のある問題が多すぎて、すべてを自分で処理することはできません。ダウンロードの途中で接続が一時的に失われることを考えてみてください...以下は、アプリから取り出した一部のコードで、不要な部分を取り除くためにわずかに変更されています。

public void downloadAndOpenPdf(String url,final File file) {
    if(!file.isFile()) {
        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        DownloadManager.Request req = new DownloadManager.Request(Uri.parse(url));
        req.setDestinationUri(Uri.fromFile(file));
        req.setTitle("Some title");

        BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                unregisterReceiver(this);
                if (file.exists()) {
                    openPdfDocument(file);
                }
            }
        };
        registerReceiver(receiver, new IntentFilter(
                DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        dm.enqueue(req);
        Toast.makeText(this, "Download started", Toast.LENGTH_SHORT).show();
    }
    else {
        openPdfDocument(file);
    }
}

public boolean openPdfDocument(File file) {
    Intent target = new Intent(Intent.ACTION_VIEW);
    target.setDataAndType(Uri.fromFile(file), "application/pdf");
    target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    try {
        startActivity(target);
        return true;
    } catch (ActivityNotFoundException e) {
        Toast.makeText(this,"No PDF reader found",Toast.LENGTH_LONG).show();
        return false;
    }

}
于 2015-12-30T06:14:13.090 に答える
0

Your code is correct. Now you need to download your pdf file to External storage or wherever you want to download and save it.

于 2015-12-30T06:00:39.673 に答える
0

このコードを削除して、もう一度お試しください。

//c.setRequestMethod("GET"); //c.setDoOutput(true); //c.getResponseCode(); //c.connect();

URL.openConnection()接続の説明は既にあると思いますので、c.connect()不要です。

于 2015-12-30T06:05:43.290 に答える