Here I posted my AssyncTask Method call DownloadFile
@Override
protected Boolean doInBackground(String... strings) {
String tMainFolder = String.valueOf(BaseApplication.getInstance().getAppContext().getDir("FileDownload", Context.MODE_PRIVATE));
tMainFolder += "/File" + mBookDetails.getBookId(); //Book download
Log.i(TAG, "Assynctask Ma" + tMainFolder);
Log.i(TAG, "book id and book name" + mBookDetails.getBookId() + mBookDetails.getBookName());
downloadBookDetails(tMainFolder, ContentfulConstants.BOOK_MAIN_IMAGE + ".png", mBookDetails.getBookMainImage());
downloadBookDetails(tMainFolder, ContentfulConstants.BOOK_MAIN_AUDIO + ".mp3", mBookDetails.getBookSound());
for (PageDetailProperties pageDetails : mBookDetails.getPageDetail()) {
String tPageNumber = pageDetails.getPageNumber().toString();
downloadBookDetails(tMainFolder, ContentfulConstants.PAGE_IMAGE + tPageNumber + ".png", pageDetails.getPageImage());
downloadBookDetails(tMainFolder, ContentfulConstants.PAGE_AUDIO + tPageNumber + ".mp3", pageDetails.getPageAudio());
downloadBookDetails(tMainFolder, ContentfulConstants.PAGE_TEXT + tPageNumber + ".txt", pageDetails.getPageText());
}
return true;
}
- I am unable to create progress count while downloading file
- As my bellow code, I create one method called "DownloadFile" in that method I have called in Assyntask doInbackround so when I try to create Dialog its show error
- so how can I implement this
private void downloadFile(String pMaolder, String pFileName, String pDowURL) {
Log.i(TAG, "Coming to this downloadBookDetails ");
try {
URL url = new URL(pDowURL);
Log.i(TAG, "pDownload URL"+ url);
URLConnection ucon = url.openConnection();
ucon.setReadTimeout(5000);
ucon.setConnectTimeout(10000);
int lenghtOfFile = ucon.getContentLength();
InputStream is = ucon.getInputStream();
BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
File directory = new File(pMainFolder, pFileName);
Log.i(TAG, "File Name dir" + directory);
FileOutputStream outStream = new FileOutputStream(directory);
byte[] buff = new byte[5 * 1024];
int len;
long total = 0;
while ((len = inStream.read(buff)) != -1) {
total += len;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total * 100)/lenghtOfFile)); //Got Error here Cannot resole publishProgress
outStream.write(buff, 0, len);
}
outStream.flush();
outStream.close();
inStream.close();
} catch (Exception e) {
//Add Network Error.
Log.i(TAG, "Download Error Exception " + e.getMessage());
e.printStackTrace();
}
}