我已經在Stack Overflow和Internet上研究瞭如何從URL
加載圖像並將其作爲Bitmap
(原本一個.png
文件)。我按照說明進行操作,代碼如下,代碼的類別爲AsyncTask
。無法從HttpUrlConnection(PNG到位圖)加載圖像,inputStream中的位圖爲空
try {
imageUrl = new URL(currentApp.getImageURL());
HttpURLConnection urlConnection = (HttpURLConnection)
imageUrl.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoInput(true);
***urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
imageBitmap = BitmapFactory.decodeStream(inputStream);
viewHolder.tvImage.setImageBitmap(imageBitmap);
inputStream.close();***
} catch (MalformedURLException e) {
Log.d(TAG, "getView: " + e.toString());
} catch (IOException e) {
Log.d(TAG, "getView: " + e.toString());
} finally {
}
爲我所用的調試工具來幫助我,我發現,從連接響應代碼是200,但imageBitmap = BitmapFactory.decodeStream(inputStream);
,我得到imageBitmap
空值。
預先感謝您!
看到logcat的,什麼是崩潰的原因。 – 2017-09-17 05:09:10
你是否在任何線程中運行代碼? – 2017-09-17 05:10:41
什麼是你的logcart消息? – 2017-09-17 05:16:06