-2

I want to be able to use url's as image sources. I have found the following function to convert to bitmaps. However, I get a "NetworkOnMainThreadException." Using the Log i found that

conn.connect();

, is what is where the functions crashes.

I also found that because of the exception I should use an AsyncTask class extension. So I used it in the second section of code below. I still ended up with the same exception. I don't completely understand what the error means just that I am trying to do too many things in the same place at the same time.

Is there an easier way to turn a url into an image I can use? How can I fix the functions I have now?

 public static Bitmap getImageBitmap(String url) {
            Log.i("bitmap","here?");
            Bitmap bm = null;
            Log.i("bitmap","here?");
            try {
                Log.i("bitmap","here?");
                URL aURL = new URL(url);
                Log.i("bitmap","here?");
                URLConnection conn = aURL.openConnection();
                Log.i("bitmap","last ?");
                conn.connect();
                Log.i("bitmap","here?");
                InputStream is = conn.getInputStream();
                Log.i("bitmap","here?");
                BufferedInputStream bis = new BufferedInputStream(is);
                Log.i("bitmap","here?");
                bm = BitmapFactory.decodeStream(bis);
                bis.close();
                Log.i("bitmap","here?");
                is.close();
                Log.i("bitmap","here?");
           } catch (IOException e) {
               Log.e("img", "Error getting bitmap", e);
           }
           return bm;
        }   

With AsyncTask:

class taskRunner extends AsyncTask<String, URL, Bitmap>{

     public static Bitmap getImageBitmap(String url) {
            Log.i("bitmap","here?");
            Bitmap bm = null;
            Log.i("bitmap","here?");
            try {
                Log.i("bitmap","here?");
                URL aURL = new URL(url);
                Log.i("bitmap","here?");
                URLConnection conn = aURL.openConnection();
                Log.i("bitmap","last ?");
                conn.connect();
                Log.i("bitmap","here?");
                InputStream is = conn.getInputStream();
                Log.i("bitmap","here?");
                BufferedInputStream bis = new BufferedInputStream(is);
                Log.i("bitmap","here?");
                bm = BitmapFactory.decodeStream(bis);
                bis.close();
                Log.i("bitmap","here?");
                is.close();
                Log.i("bitmap","here?");
           } catch (IOException e) {
               Log.e("img", "Error getting bitmap", e);
           }
           return bm;
        }   

Thank you very much for taking the time to read this.

lock
  • 43
  • 2
  • 8
  • 1
    possible duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – njzk2 May 27 '14 at 18:13
  • 2
    This isn't a duplicate - the poster is aware of the issue, but having problems solving it even *after* reference to that answer. But the current problem with the question is that there is no stack trace to show *where* the exception is currently occurring - finding it will be key to fixing it. – Chris Stratton May 27 '14 at 18:15

1 Answers1

0

First of all, Don't forget the internet permission in manifest

<uses-permission android:name="android.permission.INTERNET"/>

Seconddthing is that your implementation wrong..

class BitmapTaks extends AsyncTask<ImageView, Void, Bitmap> {

protected Bitmap doInBackground(ImageView... imageview) {
   Log.i("bitmap","here?");
        Bitmap bm = null;
        Log.i("bitmap","here?");
        try {
            Log.i("bitmap","here?");
            URL aURL = new URL(urls[0]);
            Log.i("bitmap","here?");
            URLConnection conn = aURL.openConnection();
            Log.i("bitmap","last ?");
            conn.connect();
            Log.i("bitmap","here?");
            InputStream is = conn.getInputStream();
            Log.i("bitmap","here?");
            BufferedInputStream bis = new BufferedInputStream(is);
            Log.i("bitmap","here?");
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            Log.i("bitmap","here?");
            is.close();
            Log.i("bitmap","here?");
       } catch (IOException e) {
           Log.e("img", "Error getting bitmap", e);
       }
       return bm;
}

protected void onPostExecute(Bitmap b) {
   //do something after execute
}
}

The way to call this is:

new BitmapTaks().execute(url);

Should be something like that, hope that helps

EDIT:

AsyncTask - This is async structure..

In this case the param is ImageView, And you want also to pass String for the url. The Result will be Bitmap and will be set on OnPostExecute method.

I wrote down the above example in order to understand.. You can take a look in the next link:http://chrisharrington1.wordpress.com/2012/01/24/android-setting-a-downloaded-image-to-an-imageview/

Please tell me if you need anything else

Aviad
  • 1,539
  • 1
  • 9
  • 24
  • The function is not returning a Bitmap. It's return type is . When I try to assign it to a bitmap variable I am told that there is a mismatch. When I excecute the function as directed above it does not crash, but how do I get the bitmap into my variable and set it to my imageView? – lock May 27 '14 at 19:03
  • android.os.AsyncTask This is From android doc, so you are wrong, it return Bitmap and you get it on the method onPostExecute. You need to understand what AsyncTask means.. I will udpate my answer – Aviad May 27 '14 at 19:37
  • I have done some research on AsyncTask and I have a better understanding but I don't know how I would set the bitmap to my image. Can I reference an image that I created in a loop, in my AsyncTask? Can I return my bitmap from the AsyncTask and then set it to my image? – lock May 29 '14 at 16:31
  • I am creating rows with a varying number of images based on an xml file on a server, if that information helps. – lock May 29 '14 at 16:33
  • You can pass to the AsyncTask an Handler and The global List that you need.. There are few options. Take a look on the answer on this thread: http://stackoverflow.com/questions/3090650/android-loading-an-image--from-the-web-with-asynctask Or this Thread: http://stackoverflow.com/questions/7729133/using-asynctask-to-load-images-in-listview – Aviad May 29 '14 at 18:05