中断BitmapFactory.de codeStream [英] Interrupting BitmapFactory.decodeStream

查看:254
本文介绍了中断BitmapFactory.de codeStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个工作线程调用它 BitmapFactory.de codeStream 一些连接流,像这样的:

Let's assume we have a worker thread which calls BitmapFactory.decodeStream with some connection stream, like this:

Bitmap bitmap = BitmapFactory.decodeStream(new URL(imgUrl).openConnection().getInputStream(), null, someUnrelatedOptions);

现在,我们需要以收回 BitmapFactory 已分配的任何内存immedietely终止本线程。我们不能等待它完成得太久,因为内存是由UI线程需要。这个例子是简化了,所以让我们不要赘述了为什么有许多工作要做这种方式。我正在寻找一个解决方案,以这种方式,而不是另一种方法。

Now we need to terminate this thread immedietely in order to reclaim any memory that BitmapFactory has allocated. We can't wait for it to finish for too long, as the memory is required by UI thread. The example is simplified, so let's don't go into details why it has to be done this way. I'm looking for a solution to this approach, not for another approach.

我们不能狠狠地宰线程,因为它是做一些其他的事情也是一样,有一个机会,我们会泄露一些东西。只有干净的路要走是使用 workerThread.interrupt()

We can't brutally kill the thread, as it's doing some other things too and there's a chance we will leak some stuff. The only clean way to go is to use workerThread.interrupt().

Unfortunetely的BitmapFactory并不关心线程是否被中断,继续做它的工作。很显然,我检查 Thread.currentThread()。isInterrupted()它的存在之后,但它不是足够。在很慢的互联网连接,可能需要几年的时间来加载,这将冻结该应用程序的映像(请记住,UI线程正在等待)。

Unfortunetely, the BitmapFactory doesn't care whether the thread was interrupted and continue to do it's work. Obviously I'm checking the Thread.currentThread().isInterrupted() right after it exists, but it's not sufficient. On really slow internet connections it may take years to load the image which will freeze the application (remember, UI thread is waiting).

所以,问题是 - 如何使 BitmapFactory 当线程被中断停止?我的想法是包裹的InputStream 自定义 FilterInputStream中的,覆盖每一个功能(没有很多,实际上)和在他们每个人的检查线程是否被中断。如果是的话,抛出一些 IOException异常。因为它试图从的InputStream 拉一些数据这样 BitmapFactory 将尽快停止,它应该是足够了。是否有更好的办法?如果没有,我应该叫的close()投掷前的数据流 IOException异常

So the question is - how do I make BitmapFactory stop when the thread is interrupted? My idea is to wrap the InputStream in custom FilterInputStream, overwrite every function (there aren't many, actually) and in each of them check whether the thread is interrupted. If yes, throw some IOException. This way BitmapFactory will stop as soon as it tries to pull some data from the InputStream and it should be enough. Are there any better ways? If no, should I call close() on the stream before throwing IOException?

和一张小纸条:正在等待更多的内存显示UI线程一个不错的进度,所以如果它需要一两秒钟的它不是一个问题工作线程终止。

And a little note: the UI thread that is waiting for additional memory is displaying a nice ProgressBar, so it's not a problem if it takes a second or two for the worker thread to terminate.

谢谢!

推荐答案

我觉得你可以叫someUnrelatedOptions-> requestCancelDe code()在UI线程取消解码,虽然不能保证取消德code。

I think you can call someUnrelatedOptions->requestCancelDecode() in the UI thread to cancel the decoding, although it is not guaranteed to cancel the decode.

谷歌关键字bitmapfactory requestCancelDe code德codeStream网址OpenConnection的(不带引号),你可以找到样本code,例如在<一个href=\"http://www.java2s.com/Open-Source/Android/android-platform-apps/Gallery3D/com/cooliris/media/UriTexture.java.htm\"相对=nofollow> Android的Gallery3D应用。

Google keywords "bitmapfactory requestCancelDecode decodeStream url openconnection" (without the quotes) you can find sample code, for example in Android Gallery3D app.

问候

紫藤陈

这篇关于中断BitmapFactory.de codeStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆