moagrius / TileView

TileView is a subclass of android.view.ViewGroup that asynchronously displays, pans and zooms tile-based images. Plugins are available for features like markers, hotspots, and path drawing.
MIT License
1.46k stars 337 forks source link

DownSample Bitmap Http Decoder Exception ? #45

Closed kwent closed 10 years ago

kwent commented 10 years ago

Why i cannot put a Bitmap Http Decoder (BitmapDecoderHttp) on the DownSample Decoder ? via this.setDownsampleDecoder(new BitmapDecoderHttp());

I get the following exception : android.os.NetworkOnMainThreadExceptioncause it's not rendering inside a AsynTask as the Tile Rendering.

 android.os.NetworkOnMainThreadException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
        at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
        at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
        at java.net.InetAddress.getAllByName(InetAddress.java:214)
        at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
        at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
        at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
        at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
        at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
        at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
        at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
        at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
        at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
        at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
        at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
        at com.qozix.tileview.graphics.BitmapDecoderHttp.decode(BitmapDecoderHttp.java:29)
        at com.qozix.tileview.samples.SampleManager.update(SampleManager.java:51)
        at com.qozix.tileview.samples.SampleManager.onDetailLevelChanged(SampleManager.java:61)

Regards,

moagrius commented 10 years ago

Hm. Looks like that check was added to the framework in 3.0. Normally I don't run downsamples in another thread since they were (originally) supposed to be a quick/easy decode (small file, on disk).

The short answer, and short-term fix, is to disable Strict mode. Failing that, you can hack SampleManager.update to use a new Thread (or an AsyncTask).

When I get a chance, I'll try to pull that out into a separate thread myself for the core, but need to consider any possible ripples, and it's likely not something that will happen right away (if you need it immediately). If you're not sure where to start, and I can probably provide some starter code.

LMK.

moagrius commented 10 years ago

@kwent - did you get this figured out, or should i leave the issue open?

kwent commented 10 years ago

You was right. Decoding a downsample image by HTTPDecoder is useless cause the aim is to be fast ( And the downsample file to be in local ). So i guess my question was irrelevant. I close.

Regards,