如何从网址获取视频文件的大小?

时间:2019-01-04 06:56:22

标签: java android android-studio

    VideoView vidView;
    MediaController mediaController;
    LinearLayout btn_download;
    UserService userService;

       userService = ApiUtils.getUserService();
       if(mediaController == null){
          mediaController = new MediaController(ColumnPipeInstallationActivity.this);
          mediaController.setAnchorView(vidView);
       }
    vidView.setMediaController(mediaController);
    Uri vidUri = Uri.parse(url);
    vidView.setVideoURI(vidUri);<----on create of activity video will start to play. Is it possible to get size of the file.
    vidView.start();}}
  

在此处输入代码      onClick按钮是否可以将文件下载到设备?

3 个答案:

答案 0 :(得分:1)

您正在尝试获取content-length字段

如果P Sandesh Baliga的答案不起作用,请尝试this

答案 1 :(得分:0)

您可以尝试一下

public long getFileSizeFromUrl(URL url) {
    HttpURLConnection conn = null;
    try {
      conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("HEAD");
      return conn.getContentLengthLong();
    } catch (IOException e) {
       //Maybe throw the exception, or just stacktrace it. Your call
    } finally {
      if (conn != null) {
        conn.disconnect();
      }
    }
}

答案 2 :(得分:0)

使用以下代码获取buttonClick()上视频的长度

 URL url = new URL("URL_OF_YOUR_VIDEO");
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestProperty("Accept-Encoding", "identity");
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();
            int lenghtOfFile = c.getContentLength();
            Log.w("getContentLength",""+lenghtOfFile);

希望这行得通!