做开发项目有时候会犯比较低级的错误,今天折腾一个多小时,心里面那个难受啊
我先说一下,今天的这个问题,就是我们有时候在做项目的时候,需要给安装我们app的手机创建一些文件夹,需要做一些数据存储和读取!
读取文件的时候我采用了下面的这种方法:
FileInputStream inStream = context.openFileInput(fileName);
ByteArrayOutputStream outStream =
new
ByteArrayOutputStream();
byte
[] buffer =
new
byte
[
1024
];
int
len =
0
;
while
((len = inStream.read(buffer)) != -
1
){
outStream.write(buffer,
0
,len);
}
byte
[] data = outStream.toByteArray();
.......
后来把异常捕获了一下,才醒悟过来.
因为我xx没有写路径!!!!
后来果断的改成这样:
String path=Environment.getExternalStorageDirectory()+fileName;
File fi=new File(path);
FileInputStream inputStream = new FileInputStream(fi);
byte[] buffer = new byte[1024];
int len = 0;
// 写入到手机内存中
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
byte
[] data = outStream.toByteArray();
总结:没什么好说的,就是因为粗心.