我正在尝试将文件转换为字节数组格式。所选的实际路径存在于存储中,但仍然将其抛出异常为“未找到文件”。任何人都可以帮我解决这个问题吗?
感谢您宝贵的时间!..
调用文件管理器
btn_click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent, 7);
}
});
获得回复
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch (requestCode) {
case 7:
if (resultCode == RESULT_OK) {
String filePath = data.getData().getPath();
System.out.println("====== path : "+filePath);
File file = new File(filePath);
byte[] bytesArray = new byte[(int) file.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
fis.read(bytesArray); //read file into bytes[]
fis.close();
System.out.println("====== bytesArray "+bytesArray);
} catch (FileNotFoundException e) {
System.out.println("====== File Not Found.");
e.printStackTrace();
} catch (IOException e) {
System.out.println("====== Error Reading The File. IOException");
e.printStackTrace();
}
}
}
}
答案 0 :(得分:0)
添加以下等级其Apache commons gradle
compile 'org.apache.commons:commons-io:1.3.2'
然后使用此代码
byteArray = FileUtils.readFileToByteArray(file);