How to Download a Video File to Sd Card

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());
}

}

Automatically saving video files to Android SD card

The Android Market has a 50MB limit for packaged applications, so you have to have your app download the video files after the app is installed. To do that you'll need to ask for permission to write to the phone's external SD card storage in your app's manifest. For that kind of large file size, you may want to make an interface where the user can choose individual files to download. Take a look at some of the podcasting apps to see how they handle downloading large files.

How to save file from website to sdcard

You can use this method to download a file from the internet to your SD card:

public void DownloadFromUrl(String DownloadUrl, String fileName) {

try {
File root = android.os.Environment.getExternalStorageDirectory();

File dir = new File (root.getAbsolutePath() + "/xmls");
if(dir.exists()==false) {
dir.mkdirs();
}

URL url = new URL(DownloadUrl); //you can write here any link
File file = new File(dir, fileName);

long startTime = System.currentTimeMillis();
Log.d("DownloadManager", "download begining");
Log.d("DownloadManager", "download url:" + url);
Log.d("DownloadManager", "downloaded file name:" + fileName);

/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();

/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);

/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(5000);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}

/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");

} catch (IOException e) {
Log.d("DownloadManager", "Error: " + e);
}

}

You need to add the following permissions to your AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

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



Leave a reply



Submit