ANDROID:如何将视频文件下载到 SD 卡?
我在网站上有一个 .MP4 格式的视频文件,我希望用户能够通过单击链接将该视频下载到他们的 SD 卡上。有没有一种简单的方法可以做到这一点。我目前有这段代码,但它不起作用......不知道我做错了什么。感谢您的帮助!
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class VideoManager extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);}
private final String PATH = "/sdcard/download/"; //put the downloaded file here
public void DownloadFromUrl(String VideoURL, String fileName) { //this is the downloader method
try {
URL url = new URL("http://www.ericmoyer.com/episode1.mp4"); //you can write here any link
File file = new File(fileName);
long startTime = System.currentTimeMillis();
Log.d("VideoManager", "download begining");
Log.d("VideoManager", "download url:" + url);
Log.d("VideoManager", "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(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(PATH+file);
fos.write(baf.toByteArray());
fos.close();
Log.d("VideoManager", "download ready in"
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " sec");
} catch (IOException e) {
Log.d("VideoManager", "Error: " + e);
}
}
}
I have a video file on a website in .MP4 format and I want to allow the user to be able to download the video to their SD card by clicking a link. Is there an easy way to do this. I currently have this code but its not working...not sure what I am doing wrong. THanks for any help!
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class VideoManager extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);}
private final String PATH = "/sdcard/download/"; //put the downloaded file here
public void DownloadFromUrl(String VideoURL, String fileName) { //this is the downloader method
try {
URL url = new URL("http://www.ericmoyer.com/episode1.mp4"); //you can write here any link
File file = new File(fileName);
long startTime = System.currentTimeMillis();
Log.d("VideoManager", "download begining");
Log.d("VideoManager", "download url:" + url);
Log.d("VideoManager", "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(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(PATH+file);
fos.write(baf.toByteArray());
fos.close();
Log.d("VideoManager", "download ready in"
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " sec");
} catch (IOException e) {
Log.d("VideoManager", "Error: " + e);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
内存没有用完吗?
我想象视频文件非常大 - 您在写入文件之前对其进行缓冲。
我知道你的示例代码遍布互联网 - 但它不适合下载!
使用这个:
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:
切勿硬连线路径,尤其是到外部存储的路径。您的路径在许多设备上都是错误的。使用
Environment.getExternalStoragePath()
获取外部存储的根目录(可能为/sdcard
或/mnt/sdcard或其他)。
请务必使用从
Environment.getExternalStoragePath()
返回的File
对象创建子目录。最后,不要只说“但它不起作用”。我们不知道“但它不起作用”在您的情况下意味着什么。如果没有这些信息,就很难为您提供帮助。
Never hardwire a path, particularly to external storage. Your path is wrong on many devices. Use
Environment.getExternalStoragePath()
to get the root of external storage (which may be/sdcard
or/mnt/sdcard
or something else).Be sure to create your subdirectory, using the
File
object you get back fromEnvironment.getExternalStoragePath()
.And, finally, don't just say "but its not working". We have no idea what "but its not working" means in your case. Without that information, it is very difficult to help you.