写一个Android 视频下载工具类
时间: 2024-05-18 21:18:55 浏览: 11
好的,以下是一个示例代码:
```java
public class VideoDownloader {
private static final String TAG = "VideoDownloader";
public interface DownloadListener {
void onStart();
void onProgress(int progress);
void onSuccess(File file);
void onFailure(Exception e);
}
public static void download(final String url, final DownloadListener listener) {
new Thread(new Runnable() {
@Override
public void run() {
InputStream inputStream = null;
FileOutputStream outputStream = null;
HttpURLConnection connection = null;
try {
URL videoUrl = new URL(url);
connection = (HttpURLConnection) videoUrl.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setDoInput(true);
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new RuntimeException("http status code error:" + connection.getResponseCode());
}
int fileLength = connection.getContentLength();
inputStream = connection.getInputStream();
String fileName = getFileName(url);
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);
outputStream = new FileOutputStream(file);
byte[] data = new byte[4096];
long total = 0;
int count;
while ((count = inputStream.read(data)) != -1) {
total += count;
if (fileLength > 0) {
int progress = (int) (total * 100 / fileLength);
listener.onProgress(progress);
}
outputStream.write(data, 0, count);
}
listener.onSuccess(file);
} catch (Exception e) {
listener.onFailure(e);
Log.e(TAG, "download video error:" + e.getMessage());
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
if (connection != null) {
connection.disconnect();
}
} catch (IOException e) {
Log.e(TAG, "close stream error:" + e.getMessage());
}
}
}
}).start();
}
private static String getFileName(String url) {
int index = url.lastIndexOf("/");
return url.substring(index + 1);
}
}
```
使用方法:
```java
VideoDownloader.download("http://example.com/video.mp4", new VideoDownloader.DownloadListener() {
@Override
public void onStart() {
//下载开始
}
@Override
public void onProgress(int progress) {
//下载进度
}
@Override
public void onSuccess(File file) {
//下载成功
}
@Override
public void onFailure(Exception e) {
//下载失败
}
});
```
这是一个简单的视频下载工具类,通过 HttpURLConnection 发起网络请求,将视频文件下载到本地文件,支持进度回调和异常处理。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)