首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我会得到一个空的位图?

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

Stack Overflow用户
提问于 2014-10-10 12:59:18
回答 2查看 1.4K关注 0票数 0

我试着从网上获取图像。我只需使用以下代码:

代码语言:javascript
运行
复制
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;
    }
}

然后,当我不想启动这段代码时,我会这样做:

代码语言:javascript
运行
复制
this.getBitmapFromUrl("http://icons.iconarchive.com/icons/mazenl77/I-like-buttons-3a/512/Cute-Ball-Go-icon.png");

如果我确实调试,我会看到我输入了try,但它是返回的空值。

我能做什么错事?

我的最后一只原木猫

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-10 13:11:27

您的代码工作正常,但是网络操作必须在单独的线程(而不是UI线程)中完成。你给我的网址是这样的:

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2014-10-10 13:03:12

看一看:this question。为了节省你的时间,这里有一个公认的答案:

代码语言:javascript
运行
复制
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();
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26300056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档