Lene
Lene

Reputation: 519

unable to display url image (bitmap)

I am trying to display my image but it doesn't show:

public Bitmap getUserPic(String picID) {
    String imageURL;
    Bitmap bitmap = null;
    Log.d("BITMAP", "Loading Picture");
    imageURL = picID;
    Log.d("image url", picID);
    try {
        bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent());
    } catch (Exception e) {
        Log.d("BITMAP", "Loading Picture FAILED");
        e.printStackTrace();
    }
    return bitmap;
}

Logcat output: (how do i display my logcat output neater?)

10-19 17:01:50.835: D/image url(8636): http://sphotos-c.ak.fbcdn.net/hphotos-ak-prn1/c0.0.403.403/p403x403/554229_4774210003216_2100715293_n.jpg
10-19 17:01:50.840: D/BITMAP(8636): Loading Picture FAILED 10-19
17:01:50.840: W/System.err(8636): android.os.NetworkOnMainThreadException 10-19 17:01:50.840: W/System.err(8636): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
10-19 17:01:50.840: W/System.err(8636): at java.net.InetAddress.lookupHostByName(InetAddress.java:391) 10-19
17:01:50.840: W/System.err(8636): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242) 10-19
17:01:50.840: W/System.err(8636): at java.net.InetAddress.getAllByName(InetAddress.java:220) 10-19
17:01:50.840: W/System.err(8636): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71) 10-19
17:01:50.840: W/System.err(8636): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50) 10-19
17:01:50.840: W/System.err(8636): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
10-19 17:01:50.840: W/System.err(8636):     at
libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
10-19 17:01:50.840: W/System.err(8636):     at
libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 10-19
17:01:50.840: W/System.err(8636):   at
libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
10-19 17:01:50.840: W/System.err(8636):     at
libcore.net.http.HttpEngine.connect(HttpEngine.java:303) 10-19
17:01:50.840: W/System.err(8636):   at
libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
10-19 17:01:50.840: W/System.err(8636):     at
libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232) 10-19
17:01:50.840: W/System.err(8636):   at
libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
10-19 17:01:50.840: W/System.err(8636):     at
java.net.URLConnection.getContent(URLConnection.java:194) 10-19
17:01:50.840: W/System.err(8636):   at
java.net.URL.getContent(URL.java:447) 10-19 
17:01:50.840: W/System.err(8636): at jp.gr.java_conf.akabeko.testimagegallery.MainActivity.getUserPic(MainActivity.java:275)
10-19 17:01:50.845: W/System.err(8636): at jp.gr.java_conf.akabeko.testimagegallery.MainActivity$fetchArticles.onPostExecute(MainActivity.java:251)
10-19 17:01:50.845: W/System.err(8636): at jp.gr.java_conf.akabeko.testimagegallery.MainActivity$fetchArticles.onPostExecute(MainActivity.java:1)
10-19 17:01:50.845: W/System.err(8636): at android.os.AsyncTask.finish(AsyncTask.java:602) 10-19 17:01:50.845:
W/System.err(8636): at android.os.AsyncTask.access$600(AsyncTask.java:156) 10-19
17:01:50.845: W/System.err(8636): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
10-19 17:01:50.845: W/System.err(8636): at android.os.Handler.dispatchMessage(Handler.java:99) 10-19
17:01:50.845: W/System.err(8636): at android.os.Looper.loop(Looper.java:137) 10-19 
17:01:50.845: W/System.err(8636): at android.app.ActivityThread.main(ActivityThread.java:4517) 10-19
17:01:50.845: W/System.err(8636): at java.lang.reflect.Method.invokeNative(Native Method) 10-19
17:01:50.845: W/System.err(8636): at java.lang.reflect.Method.invoke(Method.java:511) 10-19 
17:01:50.845: W/System.err(8636): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:995)
10-19 17:01:50.845: W/System.err(8636): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 10-19
17:01:50.845: W/System.err(8636): at dalvik.system.NativeStart.main(Native Method) 10-19 17:01:50.915: D/CLIPBOARD(8636): Hide Clipboard dialog at Starting input: finished by someone else... !

Upvotes: 1

Views: 550

Answers (2)

Daryn
Daryn

Reputation: 768

Try this:

AQuery aq = new AQuery(getActivity());
        aq.id(view.findViewById(R.id.image)).image(imageUrl, true, true, 0,  0,
                new BitmapAjaxCallback() {
                    @Override
                    public void callback(String url, ImageView iv, Bitmap bm, AjaxStatus status){
                        iv.setImageBitmap(bm);
                    }
                }.header("User-Agent", "android"));

Upvotes: 0

Vrashabh Irde
Vrashabh Irde

Reputation: 14367

The exception is thrown when an application attempts to perform a networking operation on its main thread.This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged.

Use an AsyncTask to get the image on a seperate thread and then assign it to display on onPostExecute and Voila! it should work.

Refer this for Async task: http://android-developers.blogspot.in/2009/05/painless-threading.html

There is a dirty fix but I would recommend never to use this

 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy); 

Upvotes: 2

Related Questions