Android 如何更快地将视频上传到php服务器
我甚至捕获了3MB视频,并将其保存到我的android设备SD卡中,我从设备中选择它,并希望将其传输到php服务器。上传效果很好,但需要很长时间,即使是3mb的视频上传也需要4-5分钟,即使我有快速的互联网连接。那么有什么方法可以更快地完成上传任务呢?或者任何其他的方法。如有其他建议,我们将不胜感激。提前感谢,:) 这是我上传的类连接和解析视频Android 如何更快地将视频上传到php服务器,android,upload,video-processing,Android,Upload,Video Processing,我甚至捕获了3MB视频,并将其保存到我的android设备SD卡中,我从设备中选择它,并希望将其传输到php服务器。上传效果很好,但需要很长时间,即使是3mb的视频上传也需要4-5分钟,即使我有快速的互联网连接。那么有什么方法可以更快地完成上传任务呢?或者任何其他的方法。如有其他建议,我们将不胜感激。提前感谢,:) 这是我上传的类连接和解析视频 try { FileInputStream fileInputStream = new FileInputStream(sour
try {
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(UPLOAD_URL);
conn = (HttpURLConnection) url.openConnection();
// conn.setFixedLengthStreamingMode(1024);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("myFile", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"myFile\";filename=\"" + fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
Log.i("Huzza", "Initial .available : " + bytesAvailable);
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
serverResponseCode = conn.getResponseCode();
Toast.makeText(_context,"response code is " +serverResponseCode,Toast.LENGTH_SHORT).show();
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
if (serverResponseCode == 200) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(conn
.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
} catch (IOException ioex) {
}
return sb.toString();
}else {
return "Could not upload";
}
}
}这是我的主要活动的背景任务
private void uploadVideo() {
class UploadVideo extends AsyncTask<Void, Void, String> {
ProgressDialog uploading;
@Override
protected void onPreExecute() {
super.onPreExecute();
uploading = ProgressDialog.show(MainActivity.this, "Uploading File", "Please wait...", false, false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
uploading.dismiss();
textViewResponse.setText(Html.fromHtml("<b>Uploaded at <a href='" + s + "'>" + s + "</a></b>"));
textViewResponse.setMovementMethod(LinkMovementMethod.getInstance());
}
@Override
protected String doInBackground(Void... params) {
Upload u = new Upload();
String msg = u.uploadVideo(selectedPath,getApplicationContext());
return msg;
}
}
UploadVideo uv = new UploadVideo();
uv.execute();
}
private void uploadVideo(){
类UploadVideo扩展异步任务{
进程对话框上传;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
Upload=ProgressDialog.show(MainActivity.this,“上载文件”,“请稍候…”,false,false);
}
@凌驾
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
上传。驳回();
textViewResponse.setText(Html.fromHtml(“上载于”);
textViewResponse.setMovementMethod(LinkMovementMethod.getInstance());
}
@凌驾
受保护字符串doInBackground(无效…参数){
Upload u=新上传();
字符串msg=u.uploadVideo(selectedPath,getApplicationContext());
返回味精;
}
}
UploadVideo uv=新的UploadVideo();
uv.execute();
}
将视频上传到服务器后,您需要压缩视频文件。我还使用了压缩,它也需要更多的时间来压缩。我不明白为什么3 mb的视频需要这么长时间。如果我使用压缩,那么它也需要更多的时间来压缩,然后上传检查你在ookla中的上传速度,然后粘贴到这里。我的平均上传速度是0.40mbps。下载速度是7mbps。电话给我?