且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ANDROID:如何下载视频文件到SD卡?

更新时间:2022-10-20 20:40:26

不运行内存? 我想一个视频文件非常大 - 这你写入文件之前,缓冲

我知道你的榜样code是所有在互联网上 - 但它的坏下载! 使用这样的:

 私人最终诠释TIMEOUT_C​​ONNECTION = 5000; // 5秒
私人最终诠释TIMEOUT_SOCKET = 30000; // 30秒


            网址URL =新的URL(IMAGEURL);
            长的startTime = System.currentTimeMillis的();
            Log.i(TAG,图片下载开头:+ IMAGEURL);

            //打开到URL的连接。
            URLConnection的UCON = url.openConnection();

            //超时会影响需要多长时间的应用程序,以实现有一个连接问题
            ucon.setReadTimeout(TIMEOUT_C​​ONNECTION);
            ucon.setConnectTimeout(TIMEOUT_SOCKET);


            //定义InputStreams从URLConnection的阅读。
            //使用3KB下载缓冲区
            InputStream的是= ucon.getInputStream();
            的BufferedInputStream inStream中=新的BufferedInputStream(是,1024 * 5);
            FileOutputStream中outStream =新的FileOutputStream(文件);
            byte []的BUFF =新的字节[5 * 1024];

            //读取字节(并存储它们),直到有没有更多阅读(-1)
            INT LEN;
            而((LEN = inStream.read(BUFF))!= -1)
            {
                outStream.write(BUFF,0,len个);
            }

            //清理
            outStream.flush();
            outStream.close();
            inStream.close();

            Log.i(TAG,下载完成
                    +((System.currentTimeMillis的() - 的startTime)/ 1000)
                    +秒); 5
 

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

        }
}

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