-1

When I coded my android project, I need to load a bitmap object with URL. There was an exception thrown when I ran following code:

Bitmap bm = GetLoginUserInfoUtil.getBitmap(object.getString("profile_image_url"));

The exception:

12-30 21:01:14.440: W/System.err(6025): android.os.NetworkOnMainThreadException
12-30 21:01:14.440: W/System.err(6025): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)

Here is my method getBitmap:

public static Bitmap getBitmap(String biturl) {

    Bitmap bitmap=null;

    try {
        URL url = new URL(biturl);

        URLConnection conn = url.openConnection();
        InputStream in = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(new BufferedInputStream(in));

    } 
    catch (Exception e) {

        e.printStackTrace();
    }
    return bitmap;


}

I found a similar posted questions regarding android.os.NetworkOnMainThreadException, the most recommended solution is using AsyncTask.

How can I implement AsyncTask in my class GetLoginUserInfoUtil?

Edit: This is the original method I load bitmap file:

public static void reqUserInfo(final Oauth2AccessToken accessToken, long uid) {

    user = new LoginUserInfo();
    UsersAPI userapi = new UsersAPI(accessToken);

    userapi.show(uid, new RequestListener() {

        public void onComplete(String arg0) {

            JSONObject object;

            try {
                object = new JSONObject(arg0);

                Bitmap bm = GetLoginUserInfoUtil.getBitmap(object.getString("profile_image_url"));
                GetLoginUserInfoUtil.user.setUserIcon(bm);
                GetLoginUserInfoUtil.user.setIsDefault("0");
                GetLoginUserInfoUtil.user.setToken(accessToken.getToken());
                GetLoginUserInfoUtil.user.setUserName(object.getString("screen_name"));

            } 
            catch (JSONException e) {

                e.printStackTrace();
            }


        }

now I have added suggested AsyncTask in the class:

private class GetBitmapTask extends AsyncTask<String, Void, Bitmap> {

    @Override
    protected Bitmap doInBackground(String... params) {
        return GetLoginUserInfoUtil.getBitmap(params[0]);
    }

    @Override
    protected void onPostExecute(Bitmap result) {

        GetLoginUserInfoUtil.user.setUserIcon(result);
    }

}

in onComplete, i have added:

new GetBitmapTask().execute(object.getString("profile_image_url"));

but i got error:

No enclosing instance of type GetLoginUserInfoUtil is accessible. Must qualify the allocation with an enclosing instance of type 
GetLoginUserInfoUtil (e.g. x.new A() where x is an instance of GetLoginUserInfoUtil).
Rui Huang
  • 382
  • 1
  • 5
  • 18
  • 3
    http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception covers this. Or, better yet, use any one of a number of third-party libraries that will load images asynchronously, such as [Universal Image Loader](https://github.com/nostra13/Android-Universal-Image-Loader) or [Picasso](https://square.github.io/picasso). – CommonsWare Dec 30 '14 at 20:10
  • There are too many question related to this topic. See this http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – Rohit5k2 Dec 30 '14 at 20:12

1 Answers1

0
private class GetBitmapTask extends AsyncTask<String, Void, Bitmap> {

    @Override
    protected Bitmap doInBackground(String... params) {
        return GetLoginUserInfoUtil.getBitmap(params[0]);
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        bm = result;
    }

}

To use it:

new GetBitmapTask(object.getString("profile_image_url")).execute();
grig
  • 848
  • 6
  • 15
  • This by itself will not work, as it is not synchronous to the program flow (cannot "return" the bitmap) in the way that the original was. – Chris Stratton Dec 30 '14 at 20:15
  • This is what i am also thinking, i need to start getting the bitmap only after getting the object, and interpret the url from the object. how to start Asynctask from here? – Rui Huang Dec 30 '14 at 21:04