Java InputSream hascode is different each time even when the request is same
Asked Answered
D

1

1

I have a query not particular to UniversalImageLoader but because i am trying to use the content stream to load the image i am facing following issue.

I am using input stream "stream://" to be able to use ImageLoader. Everything is working fine except that the hashcode of the input stream for the same request is generated differently and hence forces the imageloader to download the image again from the network instead of the disk.

What should i do to fix it.

PS: I tried to follow the answer from here

The code to get InputStream is (The UtilityMethod is noting just an async task):

 public void displayContentImage(final String fileId, final ImageView imageView) {
    UtilityMethods.startMyTask(new AsyncTask<Object, Void, InputStream>() {

        @Override
        protected InputStream doInBackground(Object... params) {
            CMServiceGateway cmServiceGateway = new CMServiceGateway();
            final InputStream inputStream = cmServiceGateway.GetContentAsStream(fileId);
            if (inputStream != null) {
                //String imageId = "stream://" + inputStream.hashCode();
                //Log.d("ImageId :: 1 ::", "file id : " + fileId + "hashcode:  " + imageId);
                //String imageId2 = "stream://" + cmServiceGateway.GetContentAsStream(fileId).hashCode();
                //Log.d("ImageId :: 2 ::", "file id : " + fileId + "hashcode:  " + imageId2);
                return inputStream;
            }
            return null;
        }

        @Override
        protected void onPostExecute(InputStream inputStream) {
            if (inputStream != null) {
                displayImage(inputStream, imageView);
            }
        }
    });
}
Dutton answered 23/9, 2016 at 6:46 Comment(3)
Can you show how do you get your image stream? Where from?Parrisch
You are opening streams and comparing their hashValue as far as I can see. IDK the contract for stream hashes, but for normal objects it is just their adress. New Stream = new Object = new Adress. You can't compare them by hashCode if my theory is correct. You have to write your own comparison method or send a md5 of the image upfront so that you can compare the md5 of the network image with the local copy md5.Enallage
@Enallage thanks for you comments this might be true for two different objects. But if you notice that the inputStream object in both the Log.d are same. Its like calling two hascode of same object one after another and you get two different answers. I am not really sure if this is how hash code works.Dutton
D
1

The basic problem of hash code equal like the sample above where you will find that the second log hash code is different than first log hash code even if the inputStream object used is same.

 //String imageId = "stream://" + inputStream.hashCode();
            //Log.d("ImageId :: 1 ::", "file id : " + fileId + "hashcode:  " + imageId);
            //String imageId2 = "stream://" + cmServiceGateway.GetContentAsStream(fileId).hashCode();
            //Log.d("ImageId :: 2 ::", "file id : " + fileId + "hashcode:  " + imageId2);

Although not answering the above question exactly but I am able to resolve this problem by not passing the hashcode to the universal image loader as id. Instead i passed the unique file Id's and it worked well now to display image from memory cache.

Dutton answered 2/12, 2016 at 6:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.