java - 此块是否选择图像或其路径?
问题描述
我了解此代码通过路径找到照片,但我不明白照片变量中的内容
fileName = f.getAbsolutePath();
try {
File image2 = new File(fileName);
FileInputStream fis = new FileInputStream(image2);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for(int readNum; (readNum=fis.read(buf))!=-1;) {
bos.write(buf, 0, readNum);
}
photo = bos.toByteArray();
} catch(Exception e1) {
JOptionPane.showMessageDialog(null, e1);
}
解决方案
下面的代码只是获取该照片的字节数组,即字节数组格式的照片内容。
fileName = f.getAbsolutePath();
try {
//Getting file object from the filePath
File image2 = new File(fileName);
// create input stream for that image object
FileInputStream fis = new FileInputStream(image2);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
// below repeated logic read the fis object till its empty and write into byte array object
for(int readNum; (readNum=fis.read(buf))!=-1;) {
bos.write(buf, 0, readNum);
}
photo = bos.toByteArray();
} catch(Exception e1) {
JOptionPane.showMessageDialog(null, e1);
}
推荐阅读
- matlab - 关于在给定间隔之间找到函数的绝对最大值的Matlab问题
- php - 在我的本地主机中的 laravel 中使用 google smtp 发送验证电子邮件
- ios - 在属性 SwiftUI 中存储 Firebase 侦听器有什么好处?
- python - 带有 python 解释器 incygwin 的红色编辑器 Eclipse
- reactjs - http-proxy-middleware 在反应生产构建中不起作用我正在为 WordPress post API 使用代理中间件
- swift - 如何使用完成按钮制作两个 UIPickerView
- jquery - 引导表(高级搜索)- 单击自定义按钮时显示所有行
- serialization - 如何在 BTreeMap 中使用未实现 PartialOrd/Ord 的项目?
- flutter - Flutter 从 Firebase 存储中获取文件
- android - 映射到数据类中的kotlin空句柄