I am downloading and saving PDF file in android using this code :
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet();
HttpResponse response;
FileOutputStream fileOutputStream;
/*We will write the file to external storage.
If External Storage is not available, then we use internal storage
*/
ApplicationLevel appLevel=(ApplicationLevel) context;
if(appLevel.isExternalStorageReadable() && appLevel.isExternalStorageWritable())
file=new File(context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS),"example.pdf");
else
file=new File(context.getFilesDir(),"example.pdf");
String path;
android.util.Log.v(TAG,"strings[0] : "+strings[0]);
try {
URI uri = new URI("www.getpdf.com");
request.setURI(uri);
response = httpclient.execute(request);
InputStream in = response.getEntity().getContent();
String inputLine;
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
android.util.Log.v(TAG, "starting content reading..." );
while ((in.read(buf)) > -1) {
fileOutputStream.write(buf);
}
android.util.Log.v(TAG,"Content Reading done.");
in.close();
fileOutputStream.close();
} catch (URISyntaxException e) {
android.util.Log.v(TAG, e.toString());
return false;
} catch (IOException e) {
android.util.Log.v(TAG, e.toString());
return false;
}
The downloaded pdf is not proper I think. When I try to open the pdf through "AdobeAcrobat" on my phone, it works sometimes and sometimes it is not able to render the pdf.
Am I downloading the pdf correctly?
This is the header of php which is returning the PDF
header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
header("Cache-Control: public");
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: Binary");
header("Content-Length:".filesize($attachment_location));
header("Content-Disposition: inline; filename=$_GET[get].pdf");