我正在使用良好的老式HttpURLConnection
来获取位图(这是异步处理的),如下所示:
Bitmap bmp;
boolean fetchOk = false;
try {
URL url = new URL(strURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(timeoutConnectMs);
connection.setReadTimeout(timeoutReadMs);
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
inputStream = connection.getInputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
bmp = BitmapFactory.decodeStream(inputStream, null, options);
fetchOk = true;
}
} catch (Exception e) {
// I also catch and handle specific Exceptions differently but there is also a catch-all here
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (!fetchOk) {
// handle retry
return;
}
// do stuff with fetched bmp
只是偶尔,获取的位图不完整......它会在中途停止并且下部是空白的。或者有时它完全是空白的。这个位图最终出现在一个小部件中,我有时会在其中看到这些不完整/空白的位图。
我认为当手机在睡眠后醒来时会发生这种情况,并且我有用户报告过类似的情况。当它发生在我身上时,日志中有以下内容:
D/My_Stuff(11449): isBitmapInCache: true
D/My_Stuff(11449): getViewId for widget_port
D/My_Stuff(11449): nothingToDo: false
W/System.err(11449): javax.net.ssl.SSLException: Read error: ssl=0x73401b5d88: I/O error during system call, Software caused connection abort
W/System.err(11449): at com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method)
W/System.err(11449): at com.android.org.conscrypt.NativeSsl.read(NativeSsl.java:411)
W/System.err(11449): at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:583)
W/System.err(11449): at com.android.okhttp.okio.Okio$2.read(Okio.java:145)
W/System.err(11449): at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:213)
W/System.err(11449): at com.android.okhttp.okio.RealBufferedSource.read(RealBufferedSource.java:61)
W/System.err(11449): at com.android.okhttp.internal.http.Http1xStream$FixedLengthSource.read(Http1xStream.java:602)
W/System.err(11449): at com.android.okhttp.okio.RealBufferedSource.read(RealBufferedSource.java:61)
W/System.err(11449): at com.android.okhttp.internal.http.HttpEngine$2.read(HttpEngine.java:1007)
W/System.err(11449): at com.android.okhttp.okio.RealBufferedSource$1.read(RealBufferedSource.java:397)
W/System.err(11449): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
W/System.err(11449): at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:875)
W/System.err(11449): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:850)
W/System.err(11449): at com.myapp.thisis.MyGetStuffTask.call(MyGetStuffTask.java:277)
W/System.err(11449): at com.myapp.thisis.MyGetStuffTask.call(MyGetStuffTask.java:31)
W/System.err(11449): at com.myapp.thisis.TaskRunner.lambda$executeAsync$1$TaskRunner(TaskRunner.java:61)
W/System.err(11449): at com.myapp.thisis.-$$Lambda$TaskRunner$-iWF_J4qwWx8b2L_xWSWxOSc0Nk.run(Unknown Source:6)
W/System.err(11449): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
W/System.err(11449): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
W/System.err(11449): at java.lang.Thread.run(Thread.java:919)
D/skia (11449): ---- read threw an exception
D/My_Stuff(11449): showMessageInWidget (Finishing up…)
D/My_Stuff(11449): getTextViewId for message_port
在 之前和之后都有正常的日志记录SSLException
,并且该过程在异常之后正常完成,只是结果是不完整的位图。换句话说,该块SSLException
似乎没有被捕获catch
,因此似乎没有任何方法可以捕获并处理它。
我认为这可能与这个问题有关(我知道它HttpURLConnection
在okhttp
Android 4.4+ 的底层使用,因此尽管没有直接使用该库,但为什么okhttp
会出现在我的日志中)。okhttp
在那里,他们还将问题与打盹模式联系起来。但是那里也没有解决这个问题。