简体   繁体   中英

ImageIO.read(InputStream) returns null

I am facing a problem which I could not find a solution. So request to everyone reading this. Please help me in this regard.

I am saving images file from ".png" files to apache derby db as Blob format. Files saved without any problem.

To read back from derby I first get data as Blob and then convert them into InputStream and then store it into my created class “Person” object using the following code

Blob patientPhoto = rs.getBlob("photo");

person.setPhoto(patientPhoto.getBinaryStream());

An overview of Class Person is shown below

 Public class Person {
    private String personName;
    private InputStream photo;

public void setPhoto(InputStream photo){
        this.photo = photo;
    }
Public InputStream getPhoto(){
    return photo;
    }
 }

Initially there are four objects of Person I have stored in derby with images. Then I retrieve these four person object from derby and store them into Vector array. I start displaying them one by one by taking from vector array as shown below

Method-1: Initialization method of person object from vector array

Person data = new Person();

int i=0;

data = vector[i++];

Method-2: display method of person photo

InputStream img = data.getPhoto();

// The image recieved from db should be buffered as it is not real file
// but bytes of streams

BufferedImage buffImg = null
    if(img!=null){
      try {
        buffImg = ImageIO.read(img);
      } catch (IOException e) {
         e.printStackTrace();
      }

    //a panel with JLabel where Image will be displayed

    pnlImg.setImage(buffImg);
    }

try {
    img.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

All four photos retrieve from vector array from index 0 to 3 showed fine but when I try to get person object backward from vector index 2 to 0 then a null pointer exception is thrown. The code ImageIO.read(img); return null value while “img” is not null .

I am unable to understand why it shown null while “img” is not null. While going from vector index 0 to 3 it working and then going backward it do not. Please help me

I believe that the Blob's InputStream is only valid while you are currently positioned on that record. So you shouldn't expect that the value you get from getBinaryStream() will remain valid for a long period of time.

Instead, your application should read the bytes out of the stream as soon as you get the blob, then you can advance to the next record in your result set.

So, change your Photo class so that the setPhoto() method reads the data from the InputStream into its own array of bytes, and then you can return your own ByteArrayInputStream to the Photo's private array of bytes when getPhoto() is called

I know that this is an old post, but if you are having a similar problem you can try this. I too had a similar issue and was able to work it out in the following fashion. So, you can have class Person changed to:

Public class Person {
    private String personName;
    private BufferedImage photo;

    public void setPhoto(BufferedImage photo){
        this.photo = photo;
    }
    public BufferedImage getPhoto(){
        return photo;
    }

}

and then when reading from the blob, simply convert the Inputstream to BufferedImage before setting it to person object as follows:

Blob patientPhoto = rs.getBlob("photo");
InputStream ins = patientPhoto.getBinaryStream();
person.setPhoto(ImageIO.read(ins));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM