Search code examples
androidpdfinputstreamhttpurlconnectionfileoutputstream

InputStream returns empty file when downloading a PDF


So I'm trying to download a PDF file using HttpURLConnection and I think I've done everything right with the input- and outputstreams, yet when I open the downloaded PDF file (using the built in File Manager in Android and/or ADB), or just inspect it by transferring it to OS X, it's completely empty and the size shows 0 bytes.

The site I'm trying to download the PDF from is:

http://www.pdf995.com/samples/pdf.pdf

Here's my code

    public static void DownloadFile(final String fileURL, final File directory) {

    Thread thread = new Thread(new Runnable(){
        @Override
        public void run() {
            try {

                try {
                    FileOutputStream f = new FileOutputStream(directory);
                    URL u = new URL(fileURL);
                    c = (HttpURLConnection) u.openConnection();
                    c.setRequestMethod("GET");
                    //c.setRequestProperty("Content-Type", "application/pdf");
                    //c.setDoOutput(true);
                    c.connect();
                    Log.d("debugz", Integer.toString(c.getContentLength()));

                    InputStream in = c.getInputStream();

                    byte[] buffer = new byte[1024];
                    int len1 = 0;
                    while ((len1 = in.read(buffer)) > 0) {
                        f.write(buffer, 0, len1);
                    }
                    f.flush();
                    f.getFD().sync();
                    f.close();


                } catch (Exception e) {
                    e.printStackTrace();
                }


            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    thread.start();


}


public static String getPDF(String pdfurl) {

    String extStorageDirectory = Environment.getExternalStorageDirectory()
            .toString();
    File folder = new File(extStorageDirectory, "schematisktscheman");
    folder.mkdir();
    File file = new File(folder, "schemainfo.pdf");
    try {
        file.createNewFile();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    DownloadFile(pdfurl, file);

    return null; //added return
}

In my main activity:

SchedulePdfProcessor.getPDF("http://www.pdf995.com/samples/pdf.pdf");
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/schematisktscheman/schemainfo.pdf")));

EDIT: I ran the network code on the main thread, which threw an exception. Now creating a new thread for the downloading and it gets the example PDF (http://www.pdf995.com/samples/pdf.pdf) and puts its content in the file all fine. Thanks to @greenapps!


Solution

  • I ran the network code on the main thread, which threw an exception. Now creating a new thread for the downloading and it gets the example PDF (http://www.pdf995.com/samples/pdf.pdf) and puts its content in the file all fine.

    Thread thread = new Thread(new Runnable(){
            @Override
            public void run() {
                try {
    
                    try {
                        FileOutputStream f = new FileOutputStream(directory);
                        URL u = new URL(fileURL);
                        c = (HttpURLConnection) u.openConnection();
                        c.setRequestMethod("GET");
                        //c.setRequestProperty("Content-Type", "application/pdf");
                        //c.setDoOutput(true);
                        c.connect();
                        Log.d("debugz", Integer.toString(c.getContentLength()));
    
                        InputStream in = c.getInputStream();
    
                        byte[] buffer = new byte[1024];
                        int len1 = 0;
                        while ((len1 = in.read(buffer)) > 0) {
                            f.write(buffer, 0, len1);
                        }
                        f.flush();
                        f.getFD().sync();
                        f.close();
    
    
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    
        thread.start();