为什么我得到一个空位图?

时间:2014-10-10 12:59:18

标签: java android

我试图从互联网上获取图像。我只需使用此代码:

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

2 个答案:

答案 0 :(得分:1)

您的代码运行正常,但网络操作必须在单独的线程(而不是UI线程)中完成。这适用于您的给定网址:

HandlerThread thread = new HandlerThread("MyThread");
thread.start();
Handler handler = new Handler(thread.getLooper());
handler.post(new Runnable() {
    @Override
    public void run() {
        Bitmap myBitmap = null;
        try {
            URL url = new URL("http://icons.iconarchive.com/icons/mazenl77/I-like-buttons-3a/512/Cute-Ball-Go-icon.png");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            myBitmap = BitmapFactory.decodeStream(input);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (myBitmap == null) {
            Log.e("TAG", "Fetch failed");
        } else {
            Log.e("TAG", myBitmap.toString());
        }
    }
});

注意:使用处理程序将阻止您使用方法来获取位图。但是,您可以创建一个方法,在图像提取失败/成功时从处理程序接收回调。

答案 1 :(得分:0)

看看: this question。为了节省您的时间,以下是接受的答案:

try {

        URL url = new URL("http://www.helpinghomelesscats.com/images/cat1.jpg");
        InputStream in = url.openConnection().getInputStream(); 
        BufferedInputStream bis = new BufferedInputStream(in,1024*8);
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        int len=0;
        byte[] buffer = new byte[1024];
        while((len = bis.read(buffer)) != -1){
            out.write(buffer, 0, len);
        }
        out.close();
        bis.close();

        byte[] data = out.toByteArray();
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        imageView.setImageBitmap(bitmap);
    }
    catch (IOException e) {
        e.printStackTrace();
    }