2011-11-08 37 views
0

我有一个问题,我想从位于sdcard中的视频中读取1024字节大小的字节, 意味着我必须一次从文件读取1024个字节。我能够从视频中获取字节数,但无法将其分块显示,我不知道如何实现。请给我建议正确的解决方案。如何从android中的视频文件中读取字节

在此先感谢。

+1

代码这是一样的任何其他文件中读取 - 寻找任何Java文件输入教程。 –

回答

0
import java.io.*; 

public class FileUtil { 
    private final int BUFFER_SIZE = 1024; 

    public void readFile(String fileName) { 

     BufferedInputStream in = null; 
     try { 
      in = new BufferedInputStream(new FileInputStream(fileName)); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
      return; 
     } 

     byte[] buffer = new byte[BUFFER_SIZE]; 

     try { 
      int n = 0; 
      while ((n = in.read(buffer, 0, BUFFER_SIZE)) > 0) { 
       /* do whatever you want with buffer here */ 
      } 
     } 
     catch(Exception e) { 
      e.printStackTrace(); 
     } 
     finally { // always close input stream 
      try { 
       in.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

基于从http://www.xinotes.org/notes/note/648/

+0

代码很好,但我无法理解为什么类似的东西属于静态工具方法。 –

+0

你说得对,在这里把它变成静态是没有意义的。 – Caner

相关问题