即使请求相同,Java InputSream的hascode也会每次都不同

编程入门 行业动态 更新时间:2024-10-12 01:24:09
本文介绍了即使请求相同,Java InputSream的hascode也会每次都不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个查询不特定于 UniversalImageLoader ,但是因为我正在尝试使用内容流加载我面临的以下问题的图像.

我正在使用输入流"stream://"来使用ImageLoader.一切正常,除了针对同一请求的输入流的哈希码生成方式不同之外,因此迫使图像加载器再次从网络而不是从磁盘下载图像.

我该怎么做才能解决它.

PS:我尝试遵循这里

获取InputStream的代码是(UtilityMethod只是一个异步任务):

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

解决方案

哈希码的基本问题与上面的示例相同,在该示例中,即使inputStream对象是第二个日志哈希码也不同于第一个日志哈希码使用的是相同的.

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

尽管不能完全回答上述问题,但是我能够通过不将哈希码作为id传递给通用图像加载器来解决此问题.相反,我通过了唯一的文件ID,现在可以很好地显示来自内存缓存的图像.

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

解决方案

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.

更多推荐

即使请求相同,Java InputSream的hascode也会每次都不同

本文发布于:2023-11-23 13:39:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621646.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:也会   每次都   Java   InputSream   hascode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!