Android的:如何读取文件中的字节? [英] Android : How to read file in bytes?
本文介绍了Android的:如何读取文件中的字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获得在Android应用程序字节的文件内容。我得到的SD卡上的文件,现在想以字节为单位选定的文件。我用Google搜索,但没有这样的成功。请帮忙
下面是code得到的文件扩展名。通过这次我得到的文件,并显示在微调。在文件选择我想要得到的文件以字节为单位。
私人列表getListOfFiles(字符串路径){
文件中的文件=新的文件(路径); 过滤器的FileFilter =新的FileFilter(){ 私人最终名单,其中,字符串> EXTS = Arrays.asList(JPEG,JPG, PNG,BMP,GIF,MP3); 公共布尔接受(文件路径名){ 字符串转; 字符串路径= pathname.getPath(); 分机= path.substring(path.lastIndexOf()+1。); 返回exts.contains(分机); } }; 最终的文件[] filesFound = files.listFiles(过滤器); 名单<字符串>名单=新的ArrayList<字符串>(); 如果(filesFound =空&安培;!&安培; filesFound.length大于0){ 对于(文件文件:filesFound){ list.add(file.getName()); } } 返回列表; }
解决方案
这是一个简单的:
文件文件=新的文件(路径);
INT大小=(INT)file.length();
字节[]字节=新字节【尺寸】;
尝试 {
的BufferedInputStream BUF =新的BufferedInputStream(新的FileInputStream(文件));
buf.read(字节,0,bytes.length);
buf.close();
}赶上(FileNotFoundException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
I am trying to get file content in bytes in Android application. I have get the file in SD card now want to get the selected file in bytes. I googled but no such success. Please help
Below is the code to get files with extension. Through this i get files and show in spinner. On file selection I want to get file in bytes.
private List 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; }
解决方案
here it's a simple:
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();
}
这篇关于Android的:如何读取文件中的字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文