I'm trying to save multiple PDFs from the internet and only some of them are working. Here is my code for saving them: try { String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); File folder = new File(extStorageDirectory, "pdf"); if (!folder.exists()) { folder.mkdir(); } File file = new File(folder, item.getName() + ".pdf"); if (!file.exists()) { file.createNewFile(); FileOutputStream f = new FileOutputStream(file); URL u = new URL(item.getURL()); HttpURLConnection c = (HttpURLConnection) u.openConnection(); c.setRequestMethod("GET"); c.setDoOutput(true); c.connect(); InputStream in = c.getInputStream(); byte[] buffer = new byte[1024]; int len1; while ((len1 = in.read(buffer)) > 0) { f.write(buffer, 0, len1); } f.close(); } } catch (Exception e) { e.printStackTrace(); }
This URL worked normally: http://www.homedepot.com/catalog/pdfImages/50/50a7bca4-2c2d-4688-913f-7852b25bdeec.pdf
This one failed: http://www.coastwidelabs.com/Products/MSDS/Grease%20Lightning%20SDS_Staples.pdf
The error I'm getting is Invalid Format when trying to open the file.
I've searched all over and can't figure it out. Any help is appreciated.