0

嗨,我正在尝试从 to 获取url图像bitmap。我有安卓 4.1 设备。new URL(). open connection().getInputStream()); 当我在应用程序冻结上运行此代码时,然后强制关闭。任何想法?

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

4 回答 4

0

您正在使用 ui 线程在 ui 线程上运行网络相关操作runOnUiThread

您应该使用 aThread或使用Asynctask.

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

你可能会得到NetworkOnMainThreadException

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

在 ui 线程上加载 asynctask。

   new TheTask().execute().

异步任务

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

用于 runOnUiThread更新 ui 并在doInbackground().

  runOnUiThread(new Runnable() //run on ui thread
                 {
                  public void run() 
                  { 
                     // update ui
                  }
                 });
于 2013-06-27T09:41:03.563 回答
0

我正在这样做

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();
于 2013-06-27T09:42:24.373 回答
0

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

于 2013-06-27T09:37:56.963 回答
0

崩溃的原因可能是两件事

  • 网络操作不应该在 UI 线程上进行。请AsyncTask为此使用。//NetworkOnMainThreadException
  • 不要对位图使用强引用。使用WeakReference<Bitmap>对象 //OutOfMemoryException
于 2013-06-27T09:38:01.707 回答