Reputation: 137
I need to display a PDF document inside the mobile browsers, without asking if the user wants to download it. Using Safari (iOs) I can do it perfectly, but when I try to do the same thing using the Android's (version ICS) native browser (called "Internet") the file is downloaded, and even the download doesn't seem to work.. After the download is done, the file is shown as Download unsuccessful, and I can't open it using a PDF file reader.
The same link download works fine is all others browsers whos not mobile. I believe the problem is in some of the headers that I need to add to the response.
I'm setting the Content-Type as application/pdf and the Content-Disposition as inline;filename="report.PDF"
Is there anyone who had the same problem? Anyone who knows a solution? :)
Thanks!
Lucas
Upvotes: 2
Views: 3499
Reputation: 3505
The android browser does not have a default built in PDF support so this cannot be easily done with that.
However here are two things you could do.
1) Download and install adobe reader from the play store and then try the following code
First you create a file on the device memory and then you read the pdf data into it. "theurl" is the url of where the pdf is located
InputStream in = null;
File dst = new File(Environment.getExternalStorageDirectory()+"/myPDF.pdf");
try {
in = theurl.openStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OutputStream out = null;
try {
out = new FileOutputStream(dst);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
}
// Transfer bytes from in to out
try {
byte[] buf = new byte[100000];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent();
Uri path = Uri.fromFile(dst);
intent.setDataAndType(path, "application/pdf");
startActivity(intent);
This will download the pdf, create it in your device memory and then the intent will open it on through your application using adove reader on your device.
Another option is
2) Go to http://jaspreetchahal.org/android-how-to-open-pdf-file-within-the-browser/ and see how that guy did it
Upvotes: 2