简体   繁体   中英

Why do i get an empty bitmap?

I try to get image from internet. Simply i use this code:

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;
    }
}

Then when i wan't to launch this code i do this:

this.getBitmapFromUrl("http://icons.iconarchive.com/icons/mazenl77/I-like-buttons-3a/512/Cute-Ball-Go-icon.png");

If i do debug i see that i enter on try, but it's return empty value.

What can i do wrong?

My last log cat

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

Your code works fine, however network operations must be done in a separate thread (not the UI thread). This works for me with your given url:

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());
        }
    }
});

Note: using a handler will prevent you from having a method to fetch the Bitmap. However, you could create a method that would receive a callback from the handler when the image fetch failed/succeeded.

Have a look at: this question . To save you time, here is the accepted answer there:

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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM