Android:如何以字节为单位读取文件?

Azh*_*har 57 java android file

我试图在Android应用程序中获取以字节为单位的文件内容.我现在在SD卡中获取文件想要以字节为单位获取所选文件.我用谷歌搜索,但没有这样的成功.请帮忙

下面是获取扩展名文件的代码.通过这个我得到的文件和旋转显示.在文件选择上我想以字节为单位获取文件.

private List<String> getListOfFiles(String path) {

   File files = new File(path);

   FileFilter filter = new FileFilter() {

      private final List<String> exts = Arrays.asList("jpeg", "jpg", "png", "bmp", "gif","mp3");

      public boolean accept(File pathname) {
         String ext;
         String path = pathname.getPath();
         ext = path.substring(path.lastIndexOf(".") + 1);
         return exts.contains(ext);
      }
   };

   final File [] filesFound = files.listFiles(filter);
   List<String> list = new ArrayList<String>();
   if (filesFound != null && filesFound.length > 0) {
      for (File file : filesFound) {
         list.add(file.getName());
      }
   }
   return list;
}
Run Code Online (Sandbox Code Playgroud)

idi*_*ger 107

这里很简单:

File file = new File(path);
int size = (int) file.length();
byte[] bytes = new byte[size];
try {
    BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
    buf.read(bytes, 0, bytes.length);
    buf.close();
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

在manifest.xml中添加权限:

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)

  • 请注意,`buf.read()`可能无法读取您请求的字节数.请参阅此处的javadoc:http://docs.oracle.com/javase/6/docs/api/java/io/BufferedInputStream.html#read(byte [],int,int) (7认同)
  • 正如第一条评论所指出的那样,这个答案实际上是错误的,尽管它享有很高的声誉并且被接受。不幸的是,这是 stackoverflow 的阴暗面。 (2认同)

Sia*_*ash 19

这是一个解决方案,可以保证读取整个文件,不需要库,并且效率很高:

byte[] fullyReadFileToBytes(File f) throws IOException {
    int size = (int) f.length();
    byte bytes[] = new byte[size];
    byte tmpBuff[] = new byte[size];
    FileInputStream fis= new FileInputStream(f);;
    try {

        int read = fis.read(bytes, 0, size);
        if (read < size) {
            int remain = size - read;
            while (remain > 0) {
                read = fis.read(tmpBuff, 0, remain);
                System.arraycopy(tmpBuff, 0, bytes, size - remain, read);
                remain -= read;
            }
        }
    }  catch (IOException e){
        throw e;
    } finally {
        fis.close();
    }

    return bytes;
}
Run Code Online (Sandbox Code Playgroud)

注意:它假定文件大小小于MAX_INT字节,您可以根据需要添加处理.


Ren*_*ard 19

今天最简单的解决方案是使用Apache common io:

http://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/FileUtils.html#readFileToByteArray(java.io.File)

byte bytes[] = FileUtils.readFileToByteArray(photoFile)
Run Code Online (Sandbox Code Playgroud)

唯一的缺点是在您的build.gradle应用中添加此依赖项:

implementation 'commons-io:commons-io:2.5'
Run Code Online (Sandbox Code Playgroud)

+ 1562方法计数


las*_*ase 14

由于接受BufferedInputStream#read不能保证读取所有内容,而不是自己跟踪缓冲区大小,我使用了这种方法:

    byte bytes[] = new byte[(int) file.length()];
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    DataInputStream dis = new DataInputStream(bis);
    dis.readFully(bytes);
Run Code Online (Sandbox Code Playgroud)

阻塞直到完整读取完成,并且不需要额外的导入.

  • Dis真的帮了我很多 (4认同)