我正在做从服务器下载视频并播放从 sdcard 下载的视频的应用程序。
我在网站上有一个 .MP4 格式的视频文件和从服务器下载的视频,应该保存在 sdcard 中。
我的问题是如何从服务器下载视频。请给我一些想法和例子。
请帮我。
我正在做从服务器下载视频并播放从 sdcard 下载的视频的应用程序。
我在网站上有一个 .MP4 格式的视频文件和从服务器下载的视频,应该保存在 sdcard 中。
我的问题是如何从服务器下载视频。请给我一些想法和例子。
请帮我。
Android 自带 HTTPClient。您应该能够从HTTPClient 网站获得所需的所有信息。
示例: http ://hc.apache.org/httpcomponents-client-ga/examples.html
Android API 文档:http: //developer.android.com/reference/org/apache/http/client/package-summary.html
使用 AsyncTask 从 android 中的服务器下载 MP4 视频
public class CheckForSDCard {
//Check If SD Card is present or not method
public static boolean isSDCardPresent() {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
return true;
}
return false;
}}
if (CheckForSDCard.isSDCardPresent()) {
//check if app has permission to write to the external storage.
if (EasyPermissions.hasPermissions(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//Get the URL entered
url =videolist.get(i).optString("video") ;
new DownloadFile().execute(url);
} else {
//If permission is not present request for the same.
Utils.checkAndRequestPermissions(getActivity());
}
} else {
Toast.makeText(getActivity(),
"SD Card not found", Toast.LENGTH_LONG).show();
}
private class DownloadFile extends AsyncTask<String, String, String> {
private String fileName;
private String folder;
private boolean isDownloaded;
File directory;
/**
* Before starting background thread
* Show Progress Bar Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
Utils.show(getActivity(),"Downloading Started");
}
/**
* Downloading file in background thread
*/
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection connection = url.openConnection();
connection.connect();
// getting file length
int lengthOfFile = connection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
//String timestamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
//Extract file name from URL
fileName = f_url[0].substring(f_url[0].lastIndexOf('/') + 1, f_url[0].length());
//Append timestamp to file name
fileName = fileName;
//External directory path to save file
folder = Environment.getExternalStorageDirectory() + File.separator + "Internal Storage/DCIM/Camera/";
//Create androiddeft folder if it does not exist
directory = new File(folder);
if (!directory.exists()) {
directory.mkdirs();
}
// Output stream to write file
OutputStream output = new FileOutputStream(folder + fileName);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress("" + (int) ((total * 100) / lengthOfFile));
Log.d(TAG, "Progress: " + (int) ((total * 100) / lengthOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
return "Downloaded at: " + folder + fileName;
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return "Something went wrong";
}
/**
* Updating progress bar
*/
protected void onProgressUpdate(String... progress) {
// setting progress percentage
}
@Override
protected void onPostExecute(String message) {
// dismiss the dialog after the file was downloaded
getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(directory)));
// Display File path after downloading
Toast.makeText(getActivity(),
"Downloaded Successfully", Toast.LENGTH_LONG).show();
}
}