首页 > 解决方案 > 此块是否选择图像或其路径?

问题描述

我了解此代码通过路径找到照片,但我不明白照片变量中的内容

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);
}

标签: java

解决方案


下面的代码只是获取该照片的字节数组,即字节数组格式的照片内容。

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);
}

推荐阅读