1

我正在尝试将视频发送到 android 上的 asp.net 服务器。但是,由于视频大小约为 26 MB,我无法发送视频?有没有办法将视频分成几部分并使用java通过android将它们发送到.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());
        }
      }
}
4

1 回答 1

0

首先,如果视频在重新组装之前不需要播放,只需在每个片段中发送这么多字节并让服务器重新组装它。其余部分将假设您需要将其分成多个可以单独播放的视频。

您可能会查看 ICS 中的视频编辑示例,但取决于它的实现方式可能会将您限制在最新的设备上。

否则:

大多数压缩视频格式可以被认为是绝对信息(关键帧)的重复序列,然后是相对于该基础编码的帧的相对信息。如果您了解您正在使用的未指定格式的详细信息,您可能会了解如何识别一个接近您希望剪切的位置的位置,即新部分以完整关键帧开始的位置。您将在那里拆分视频,在新部分中创建任何必要的文件头,并调整原始头中的任何长度信息。

虽然自己这样做会很有教育意义,但寻找现有的解决方案也是值得的。

于 2012-06-19T15:41:39.227 回答