Java 从URL下载视频并将其存储在android设备中

Java 从URL下载视频并将其存储在android设备中,java,android,video,Java,Android,Video,我有以下代码从URL下载视频: 字符串videoUrl=“” 私有类LongOperation扩展了异步任务{ private ProgressDialog=新建ProgressDialog(MainActivity.this); 私有最终整数超时\连接=5000;//5秒 私有最终整数超时\u套接字=30000;//30秒 私有字符串TAG=“TAG”; @凌驾 受保护的void onPreExecute(){ setMessage(“下载…”); dialog.show(); } @凌驾 受

我有以下代码从URL下载视频:

字符串videoUrl=“”

私有类LongOperation扩展了异步任务{
private ProgressDialog=新建ProgressDialog(MainActivity.this);
私有最终整数超时\连接=5000;//5秒
私有最终整数超时\u套接字=30000;//30秒
私有字符串TAG=“TAG”;
@凌驾
受保护的void onPreExecute(){
setMessage(“下载…”);
dialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
url=新url(视频url);
long startTime=System.currentTimeMillis();
Log.i(标签,“视频下载开始:”+videoUrl);
//打开到该URL的连接。
URLConnection ucon=url.openConnection();
//此超时会影响应用程序意识到存在连接问题所需的时间
setReadTimeout(连接超时);
设置连接超时(超时\u套接字);
//定义要从URLConnection读取的InputStreams。
//使用3KB下载缓冲区
InputStream=ucon.getInputStream();
BufferedInputStream inStream=新的BufferedInputStream(is,1024*5);
FileOutputStream outStream=新的FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).toString()+“/new.mp4”);
字节[]buff=新字节[5*1024];
//读取字节(并存储它们),直到没有更多的内容可读取(-1)
内伦;
而((len=流内读取(buff))!=-1){
超流写入(buff,0,len);
}
//清理
冲水;
exptream.close();
流内关闭();
Log.i(标记“下载已完成”
+((System.currentTimeMillis()-startTime)/1000)
+“sec”);
}捕获(格式错误){
e、 printStackTrace();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回“已执行”;
}
@凌驾
受保护的void onPostExecute(字符串结果){
Log.v(“测试”,结果);
dialog.dismise();
}
}

视频文件在文件夹中。但是,当我试图在媒体播放器中播放它时,它没有播放。代码有什么错误吗?

请更改
e.printStackTrace()
Log.e(标记“异常”,e)并报告logcat所说的错误。其中没有异常消息。此外,不是视频的url,而是播放视频的网页的url。如何从youtube获取url,我们可以通过该url下载视频?抱歉……这不是编程问题。尝试在stackexchange.com上进行谷歌搜索或搜索。请更改
e.printStackTrace()
Log.e(标记“异常”,e)并报告logcat所说的错误。其中没有异常消息。此外,不是视频的url,而是播放视频的网页的url。如何从youtube获取url,我们可以通过该url下载视频?抱歉……这不是编程问题。尝试在stackexchange.com上进行谷歌搜索或搜索。
private class LongOperation extends AsyncTask<String, Void, String> {
    private ProgressDialog dialog = new ProgressDialog(MainActivity.this);
    private final int TIMEOUT_CONNECTION = 5000;//5sec
    private final int TIMEOUT_SOCKET = 30000;//30sec
    private String TAG = "TAG";

    @Override
    protected void onPreExecute() {
        dialog.setMessage("Downloading...");
        dialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            url = new URL(videoUrl);
            long startTime = System.currentTimeMillis();
            Log.i(TAG, "video download beginning: " + videoUrl);

            //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(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).toString()+"/new.mp4");
            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");

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "Executed";
    }

    @Override
    protected void onPostExecute(String result) {
        Log.v("TEST", result);
        dialog.dismiss();
    }
}