ANDROID: How do I download a video file to SD card?
aren't running out of memory ?
I imagine a video file is very large - which you are buffering before writing to file.
I know your example code is all over the internet - but it's BAD for downloading !
Use this:
private final int TIMEOUT_CONNECTION = 5000;//5sec
private final int TIMEOUT_SOCKET = 30000;//30sec
URL url = new URL(imageURL);
long startTime = System.currentTimeMillis();
Log.i(TAG, "image download beginning: "+imageURL);
//Open a connection to that URL.
URLConnection ucon = url.openConnection();
//this timeout affects how long it takes for the app to realize there's a connection problem
ucon.setReadTimeout(TIMEOUT_CONNECTION);
ucon.setConnectTimeout(TIMEOUT_SOCKET);
//Define InputStreams to read from the URLConnection.
// uses 3KB download buffer
InputStream is = ucon.getInputStream();
BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
FileOutputStream outStream = new FileOutputStream(file);
byte[] buff = new byte[5 * 1024];
//Read bytes (and store them) until there is nothing more to read(-1)
int len;
while ((len = inStream.read(buff)) != -1)
{
outStream.write(buff,0,len);
}
//clean up
outStream.flush();
outStream.close();
inStream.close();
Log.i(TAG, "download completed in "
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " sec");5
How can I download a video file to SD card?
Try this Solution
package com.Video.ALLTestProject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
public class VideoSaveSDCARD extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ProgressBack PB = new ProgressBack();
PB.execute("");
}
private class ProgressBack extends AsyncTask<String,String,String> {
ProgressDialog PD;
@Override
protected void onPreExecute() {
PD= ProgressDialog.show(LoginPage.this,null, "Please Wait ...", true);
PD.setCancelable(true);
}
@Override
protected void doInBackground(String... arg0) {
downloadFile("http://beta-vidizmo.com/hilton.mp4","Sample.mp4");
}
protected void onPostExecute(Boolean result) {
PD.dismiss();
}
}
private void downloadFile(String fileURL, String fileName) {
try {
String rootDir = Environment.getExternalStorageDirectory()
+ File.separator + "Video";
File rootFile = new File(rootDir);
rootFile.mkdir();
URL url = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
FileOutputStream f = new FileOutputStream(new File(rootFile,
fileName));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = in.read(buffer)) > 0) {
f.write(buffer, 0, len1);
}
f.close();
} catch (IOException e) {
Log.d("Error....", e.toString());
}
}
Ways to Download Videos and Store in SD Card in Android
Unfortunately there is no compatibility for DownloadManager
for android 8.
The only alternative is to create your own downloader for android 8 devices.
this stackoverflow answer is a good example to follow.
How to save downloaded file on sd card from download manager programmatically on android
This is what i used.
Uri downloadUri = Uri.parse(DOWNLOAD_FILE);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setDescription("Downloading a file");
long id = downloadManager.enqueue(request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("File Downloading...")
.setDescription("Image File Download")
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "cm.png"));
Related Topics
Conversion from String to JSON Object Android
Adding Spinner to Actionbar (Not Navigation
Combining Two Bitmap Image (Side by Side)
Android: Converting Color Image to Grayscale
How to Center The Camera So That Marker Is at The Bottom of Screen? (Google Map API V2 Android)
Android Viewmodel Has No Zero Argument Constructor
Adb Shell Su Works But Adb Root Does Not
How to Replace The Activity's Fragment from The Fragment Itself
Android Studio and Gradle Build Error
How to Change System Navigation Bar Color
How to Get Thumbnail for Video in My /Sdcard/Android/Data/Mypackage/Files Folder
Remove Animation/Shifting Mode from Bottomnavigationview Android
Signing My Android Application as System App
How to Make Space Between Linearlayout Children
How to Scale a Streaming Bitmap In-Place Without Reading The Whole Image First
Android: Tint Using Drawablecompat
How to Check Which Current Image Resource Is Attached to Imageview in Android Xml