主页 > 开发文档 > android调用摄像头拍摄视频上传至服务器

android调用摄像头拍摄视频上传至服务器

1、开启手机摄像头拍摄视频命令
     Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
     intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
     startActivityForResult(intent, 1);
 
2、在回调Activity中获取视频的路径
    @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub
  super.onActivityResult(requestCode, resultCode, data);
  Log.d("test", "onActivityResult() requestCode:" + requestCode
    + ",resultCode:" + resultCode + ",data:" + data);
  if (null != data) {
   Uri uri = data.getData();
   if (uri == null) {
    return;
   } else {
    Cursor c = getContentResolver().query(uri,
      new String[] { MediaStore.MediaColumns.DATA }, null,
      null, null);//根据返回的URI,查找数据库,获取视频的路径
    if (c != null && c.moveToFirst()) {
     String filPath = c.getString(0);
     Log.d("test", filPath);
     new Upload(filPath).start();
    }
   }
  }
 }
 
三、上传视频至服务器
 
public void uploadFile(String imageFilePath) {
  String actionUrl = "http://192.168.1.26:8080/test1/servlet/Videoservlet";
  try {
   URL url = new URL(actionUrl);
   HttpURLConnection con = (HttpURLConnection) url.openConnection();
   con.setDoInput(true);
   con.setDoOutput(true);
   con.setUseCaches(false);
   con.setRequestMethod("POST");
   DataOutputStream ds = new DataOutputStream(con.getOutputStream());
   File file = new File(imageFilePath);
   FileInputStream fStream = new FileInputStream(file);
   int bufferSize = 1024;
   byte[] buffer = new byte[bufferSize];
   int length = -1;
   while ((length = fStream.read(buffer)) != -1) {
    ds.write(buffer, 0, length);
   }
   fStream.close();
   ds.flush();
   InputStream is = con.getInputStream();
   int ch;
   StringBuffer b = new StringBuffer();
   while ((ch = is.read()) != -1) {
    b.append((char) ch);
   }
   ds.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }