Android Download File With Progress Bar

This demo is created for the android developers who are facing problem to put code for download file with progress bar. Here is my small effort to do that. I have again used LoopJ Library. I used LoopJ Library in my previous blog of AsyncImageView.

Here I am putting my method and link to download full source code of example project:

public void fileDownload(String url) {
		URL = url;
		
		fileName = url.substring(url.lastIndexOf('/') + 1, url.length());

		File f = LiApplication.getFilePath(fileName, MainActivity.this);
		// if (f.exists()) {
		System.out.println("getting image from folder " + f);
		// myBitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
		// imageView.setImageBitmap(myBitmap);

		// } else {
		System.out.println("getting image from url" + f);
		progressBar.setProgress(0);
		progressBar.setVisibility(View.VISIBLE);
		LIRestClient.downloadFile(url, null, new FileAsyncHttpResponseHandler(f) {

			@Override
			public void onSuccess(int statusCode, File file) {
				Log.i(TAG, "onClick onSuccess :" + statusCode);

				Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
				imageView.setImageBitmap(myBitmap);

				// progressBar.setVisibility(View.GONE);
			}

			@Override
			public void onProgress(int bytesWritten, int totalSize) {
				int totProgress = (int) (((float) bytesWritten * 100) / totalSize);
				Log.i("Progress::::", "" + totProgress);
				if (totProgress > 0) {
					progressBar.setProgress(totProgress);
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers, Throwable e, File response) {
				response.delete();
				Log.i(TAG, "onClick onFailure :" + statusCode + " " + e.getLocalizedMessage());
				e.printStackTrace();
				// progressBar.setVisibility(View.GONE);

				int resID = getResources().getIdentifier(strDefaultImage, "drawable", c.getPackageName());
				imageView.setImageResource(resID);
			}
		});
		// }
	}

Here is Github link: Android-File-Download

 

Free SEO Checker |
Test your website for free with OnAirSEO.com

Get Your SEO report!

Don’t miss the next post!

Loading

Related Posts