0

Hi I am trying to get an image from url to bitmap. I have android 4.1 device. When I run this code on new URL(). open connection().getInputStream()); app freezes then force close. Any idea?

   runOnUiThread(new Runnable() {
            public void run() {
                String url = "http://netmera.com/cdn/app/file/netmera.com/series/img-48/1372262272227_89/medium";
                try {
                    Bitmap bmp = BitmapFactory.decodeStream(new URL(url)
                            .openConnection().getInputStream());
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
              }
Raghunandan
  • 132,755
  • 26
  • 225
  • 256

4 Answers4

0

bitmap = BitmapFactory.decodeStream((InputStream) new URL("your url").getContent());

user2462737
  • 208
  • 2
  • 11
0

The reason for the Crash can be two things

  • Network operation should not be done on the UI thread. Kindly use AsyncTask for this. //NetworkOnMainThreadException
  • Do not use strong referneces for Bitmaps. Use WeakReference<Bitmap> objects //OutOfMemoryException
blganesh101
  • 3,647
  • 1
  • 24
  • 44
0

You are running network related operation on the ui thread using runOnUiThread.

You should use a Thread or use Asynctask.

http://developer.android.com/reference/android/os/AsyncTask.html

You are probably getting NetworkOnMainThreadException

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

Load asynctask on ui thread.

   new TheTask().execute().

AsyncTask

  class TheTask extends AsyncTask<Void,Void,Void>
  {

@Override
protected Void doInBackground(Void... params) {
    // TODO Auto-generated method stub
             String url = "http://netmera.com/cdn/app/file/netmera.com/series/img-48/1372262272227_89/medium"; 
    try {
                Bitmap bmp = BitmapFactory.decodeStream(new URL(url)
                        .openConnection().getInputStream());

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
    return null;
}

@Override
protected void onPostExecute(Void result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
}
}

Use runOnUiThread to update ui and do your netowrk related operation in doInbackground().

  runOnUiThread(new Runnable() //run on ui thread
                 {
                  public void run() 
                  { 
                     // update ui
                  }
                 });
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • @user2527366 remove the `runonuithread` then. this bitmap = `BitmapFactory.decodeStream((InputStream) new URL( doctor.getPhoto()).getContent())` should be outside `runonuithread` in `doInbackground()`. you can update ui in `runonuithread`. – Raghunandan Jun 27 '13 at 09:43
0

I am doing it like this

final Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                // TODO Auto-generated method stub

                if(msg.what==1){
                    doctoImage.setImageBitmap(bitmap);// doctoImage you image view
                }
            }
        };

Thread thread = new Thread(new Runnable() {

            public void run() {
                // TODO Auto-generated method stub

                try {
                    bitmap = BitmapFactory.decodeStream((InputStream) new URL(
                            doctor.getPhoto()).getContent());

                    handler.sendEmptyMessage(1);
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });thread.start();
user2462737
  • 208
  • 2
  • 11