Hi all here i'm introducing new simple example how to download PDF file with AsyncTask, in this example you get idea how to download file, If you already downloaded it will ask view of the article with respect pdf viewers.
DownloadFileAsync.java
call this method where ever we want
new DownloadFileAsync(this).execute();
public class DownloadFileAsync extends AsyncTask<String, Integer, Integer> {
private Context mContext;
private ProgressDialog mProgressDialog;
private NotificationManager mNotifyManager;
private NotificationCompat.Builder build;
int id = 1;
private File existingfile;
private Uri pathUri;
public DownloadFileAsync(Context context) {
this.mContext = context;
pathUri = Uri.parse("file:///" + Environment.getExternalStorageDirectory().getPath() + "/download/"+ArticleTitle+".pdf");
existingfile = new File((Environment.getExternalStorageDirectory().getPath() + "/download/"+ArticleTitle+".pdf"));
if (!existingfile.exists()) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(pathUri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
mNotifyManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
build = new NotificationCompat.Builder(mContext);
build.setContentTitle("Download").setContentText("Download in progress")
.setSmallIcon(R.mipmap.ic_launcher).setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification .setContentIntent(pendingIntent).setAutoCancel(true);
} else {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(pathUri, "application/pdf");
mContext.startActivity(i);
}
}
@Override protected void onPreExecute() {
super.onPreExecute();
if (!existingfile.exists()) {
mProgressDialog = ProgressDialog.show(mContext, "", "please wait.....");
build.setProgress(100, 0, false);
mNotifyManager.notify(id, build.build());
}
}
@Override protected Integer doInBackground(String... aurl) {
if (!existingfile.exists()) {
mProgressDialog.show();
int count;
try {
URL url = new URL(PDFURL);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/download/"+ArticleTitle+".pdf");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
//total += count; output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
int i;
for (i = 0; i <= 100; i += 5) {
// Sets the progress indicator completion percentage publishProgress(Math.min(i, 100));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Log.d("Failure", "sleeping failure");
}
}
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
if (!existingfile.exists()) {
build.setProgress(100, progress[0], false);
mNotifyManager.notify(id, build.build());
super.onProgressUpdate(progress);
}
}
@Override protected void onPostExecute(Integer unused) {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
build.setContentText("Download complete");
// Removes the progress bar build.setProgress(0, 0, false);
mNotifyManager.notify(id, build.build());
super.onPostExecute(unused);
}
}
}
No comments:
Post a Comment