I am trying to write an app to download pdfs from url, store them on sd, then open by adobe pdf reader or other apps which ever able to open the pdf.
untill now, i had "successfully downloaded and stored it on Sd card" (but everytime when i try to open the pdf with a pdf reader, reader crash and say unexpected error occurs), for example, http://maven.apache.org/maven-1.x/maven.pdf
here is the code for my downloader:
//........code set ui stuff
//........code set ui stuff
new DownloadFile().execute(fileUrl, fileName);
private class DownloadFile extends AsyncTask<String, Void, Void>{
@Override
protected Void doInBackground(String... strings) {
String fileUrl = strings[0]; // -> http://maven.apache.org/maven-1.x/maven.pdf
String fileName = strings[1]; // -> maven.pdf
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File folder = new File(extStorageDirectory, "testthreepdf");
folder.mkdir();
File pdfFile = new File(folder, fileName);
try{
pdfFile.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
FileDownloader.downloadFile(fileUrl, pdfFile);
return null;
}
}
public class FileDownloader {
private static final int MEGABYTE = 1024 * 1024;
public static void downloadFile(String fileUrl, File directory){
try {
URL url = new URL(fileUrl);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(directory);
int totalSize = urlConnection.getContentLength();
byte[] buffer = new byte[MEGABYTE];
int bufferLength = 0;
while((bufferLength = inputStream.read(buffer))>0 ){
fileOutputStream.write(buffer, 0, bufferLength);
}
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
in debug mode, i can see the app downloaded it and store this pdf on /storage/sdcard/testpdf/maven.pdf, however, i guess the file may be corrupted somehow during downloading, so it doesnt open up properly...
here is the code how i intent to open it with other reader app:
File pdfFile = new File(Environment.getExternalStorageDirectory() + "/testthreepdf/" + fileName); // -> filename = maven.pdf
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try{
startActivity(pdfIntent);
}catch(ActivityNotFoundException e){
Toast.makeText(documentActivity, "No Application available to view PDF", Toast.LENGTH_SHORT).show();
}
Download source code from here (Open Pdf from url in Android Programmatically)
MainActivity.java
Thanks!
This is the best method to download and view PDF file.You can just call it from anywhere as like
PDFTools.showPDFUrl(context, url);
here below put the code. It will works fine
}
try it. it will works, enjoy
Hi the problem is in FileDownloader class
You need to remove the above two lines and everything will work fine. Please mark the question as answered if it is working as expected.
Attaching the working code with screenshots.
MainActivity.java
FileDownloader.java
AndroidManifest.xml
activity_main.xml