我正在尝试将视频发送到android上的asp.net服务器。但是,由于视频大小约为26 MB,我无法发送视频?有没有办法将视频分成几部分,并通过使用java将它们发送到.net服务器?
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
public class VideoUploader extends Activity
{
/** Called when the activity is first created. */
public static final int SELECT_VIDEO=1;
public static final String TAG="UploadActivity";
String path="";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
openGaleryVideo();
}
public void openGaleryVideo()
{
Intent intent=new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Video"),SELECT_VIDEO);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_VIDEO) {
Uri videoUri = data.getData();
path= getPath(videoUri);
doFileUpload();
}
}
}
public String getPath(Uri uri)
{
String[] projection = { MediaStore.Video.Media.DATA};
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public void doFileUpload()
{
String pathToOurFile = path;//this will be the file path
String urlServer = "http://192.168.10.177/androidweb/default.aspx";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 2*1024* 1024;
try
{
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile));
URL url = new URL(urlServer);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data");
connection.setRequestProperty("SD-FileName", "Chrysanthemum.JPEG");
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}//end of while statement
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
Log.d("ServerCode",""+serverResponseCode);
Log.d("serverResponseMessage",""+serverResponseMessage);
fileInputStream.close();
outputStream.flush();
outputStream.close();
}//end of try body
catch (Exception ex)
{
//ex.printStackTrace();
Log.e("Error: ", ex.getMessage());
}
}
}
答案 0 :(得分:0)
首先,如果视频在重新组装之前不需要播放,只需在每个片段中发送这么多字节并让服务器重新组装它。其余部分将假设您需要将其分成多个可以单独播放的视频。
您可以查看ICS中的视频编辑示例,但具体取决于它的实现方式,可能会限制您使用最新的设备。
否则:
大多数压缩视频格式可以被认为是绝对信息(关键帧)的重复序列,后面是相对于该基数编码的帧的相对信息。如果您了解所使用的未指定格式的详细信息,您可以了解如何识别您希望切割的大小的位置,其中新的部分以完整的关键帧开始。您可以在那里拆分视频,在新部件中创建任何必要的文件头,并调整原始头中的任何长度信息。
虽然自己这样做会有教育意义,但也值得寻找现有的解决方案。