-
-
Save vanleantking/68ed2471505f496dcd6719167572f902 to your computer and use it in GitHub Desktop.
Example of how to download a video using Okhttp and show a progress bar to the user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class VideoDownloader extends AsyncTask<Void, Long, Boolean> { | |
@Override | |
protected void onPreExecute() { | |
super.onPreExecute(); | |
} | |
@Override | |
protected Boolean doInBackground(Void... params) { | |
OkHttpClient client = new OkHttpClient(); | |
String url = "http://myamazingvideo.mp4"; | |
Call call = client.newCall(new Request.Builder().url(url).get().build()); | |
try { | |
Response response = call.execute(); | |
if (response.code() == 200 || response.code() == 201) { | |
Headers responseHeaders = response.headers(); | |
for (int i = 0; i < responseHeaders.size(); i++) { | |
Log.d(LOG_TAG, responseHeaders.name(i) + ": " + responseHeaders.value(i)); | |
} | |
InputStream inputStream = null; | |
try { | |
inputStream = response.body().byteStream(); | |
byte[] buff = new byte[1024 * 4]; | |
long downloaded = 0; | |
long target = response.body().contentLength(); | |
mediaFile = new File(getActivity().getCacheDir(), "mySuperVideo.mp4"); | |
OutputStream output = new FileOutputStream(mediaFile); | |
publishProgress(0L, target); | |
while (true) { | |
int readed = inputStream.read(buff); | |
if (readed == -1) { | |
break; | |
} | |
output.write(buff, 0, readed); | |
//write buff | |
downloaded += readed; | |
publishProgress(downloaded, target); | |
if (isCancelled()) { | |
return false; | |
} | |
} | |
output.flush(); | |
output.close(); | |
return downloaded == target; | |
} catch (IOException ignore) { | |
return false; | |
} finally { | |
if (inputStream != null) { | |
inputStream.close(); | |
} | |
} | |
} else { | |
return false; | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return false; | |
} | |
} | |
@Override | |
protected void onProgressUpdate(Long... values) { | |
super.onProgressUpdate(values); | |
progressBar.setMax(values[1].intValue()); | |
progressBar.setProgress(values[0].intValue()); | |
} | |
@Override | |
protected void onPostExecute(Boolean aBoolean) { | |
super.onPostExecute(aBoolean); | |
progressBar.setVisibility(View.GONE); | |
if (mediaFile != null && mediaFile.exists()) { | |
playVideo(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://square.github.io/okhttp/recipes/
https://github.com/dazza5000/OkHttpDownloadImage/blob/master/app/src/main/java/net/tipit/okhttpspike/MainActivity.java
https://stackoverflow.com/questions/21477493/android-download-manager-completed
https://stackoverflow.com/questions/7239996/android-downloadmanager-api-opening-file-after-download
https://medium.com/mobile-app-development-publication/download-file-in-android-with-kotlin-874d50bccaa2
https://imstudio.medium.com/android-downloadfile-from-server-using-retrofit-part-1-817ae8caa169
https://imstudio.medium.com/android-downloadfile-from-server-using-retrofit-part-2-beware-with-large-files-use-streaming-eba001dfc08a
https://www.freshbytelabs.com/2018/12/download-file-from-url-in-android-using.html
https://galdin.dev/blog/downloading-a-file-using-retrofit/
https://www.youtube.com/watch?v=E5fzNHJDkzM