【发布时间】:2014-10-10 21:31:32
【问题描述】:
我尝试从互联网上获取图像。我只需使用此代码:
public Bitmap getBitmapFromUrl(String src)
{
try
{
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
然后,当我不想启动此代码时,我会这样做:
this.getBitmapFromUrl("http://icons.iconarchive.com/icons/mazenl77/I-like-buttons-3a/512/Cute-Ball-Go-icon.png");
如果我进行调试,我会看到我在尝试时输入,但它返回空值。
我能做错什么?
我最后的日志猫
10-10 15:15:51.085 12935-12974/com.example.android.navigationdrawerexample W/System.err﹕ java.net.UnknownHostException: Unable to resolve host "icons.iconarchive.com": No address associated with hostname
10-10 15:15:51.085 12935-12974/com.example.android.navigationdrawerexample W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:394)
10-10 15:15:51.085 12935-12974/com.example.android.navigationdrawerexample W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-10 15:15:51.085 12935-12974/com.example.android.navigationdrawerexample W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-10 15:15:51.085 12935-12974/com.example.android.navigationdrawerexample W/System.err﹕ at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
10-10 15:15:51.095 12935-12974/com.example.android.navigationdrawerexample W/System.err﹕ at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-10 15:15:51.095 12935-12974/com.example.android.navigationdrawerexample W/System.err﹕ at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
10-10 15:15:51.095 12935-12974/com.example.android.navigationdrawerexample W/System.err﹕ at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
10-10 15:15:51.095 12935-12974/com.example.android.navigationdrawerexample W/System.err﹕ at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-10 15:15:51.095 12935-12974/com.example.android.navigationdrawerexample W/System.err﹕ at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
10-10 15:15:51.095 12935-12974/com.example.android.navigationdrawerexample W/System.err﹕ at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
10-10 15:15:51.095 12935-12974/com.example.android.navigationdrawerexample W/System.err﹕ at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
10-10 15:15:51.095 12935-12974/com.example.android.navigationdrawerexample W/System.err﹕ at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
10-10 15:15:51.095 12935-12974/com.example.android.navigationdrawerexample W/System.err﹕ at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
10-10 15:15:51.095 12935-12974/com.example.android.navigationdrawerexample W/System.err﹕ at com.example.android.navigationdrawerexample.MainActivity$PlanetFragment$1.run(MainActivity.java:311)
10-10 15:15:51.095 12935-12974/com.example.android.navigationdrawerexample W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:725)
10-10 15:15:51.095 12935-12974/com.example.android.navigationdrawerexample W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
10-10 15:15:51.095 12935-12974/com.example.android.navigationdrawerexample W/System.err﹕ at android.os.Looper.loop(Looper.java:176)
10-10 15:15:51.095 12935-12974/com.example.android.navigationdrawerexample W/System.err﹕ at android.os.HandlerThread.run(HandlerThread.java:60)
10-10 15:15:51.095 12935-12974/com.example.android.navigationdrawerexample E/TAG﹕ Fetch failed
【问题讨论】:
-
尝试使用 BitmapFactory.Options this 的 inJustDecodeBounds 选项在解码流之前计算大小。另请参阅stackoverflow.com/questions/4223472/…
-
是的,但我不知道大小..