从网址读取android 4.1图片

时间:2013-06-27 09:19:22

标签: android bitmap android-image

您好我正在尝试从urlbitmap获取图片。我有android 4.1设备。当我在new URL(). open connection().getInputStream()); app冻结时运行此代码然后强行关闭。有什么想法吗?

   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 个答案:

答案 0 :(得分:0)

bitmap = BitmapFactory.decodeStream((InputStream)new URL(“your url”)。getContent());

答案 1 :(得分:0)

崩溃的原因可能是两件事

  • 不应在UI线程上进行网络操作。请使用AsyncTask。 // NetworkOnMainThreadException
  • 不要对位图使用强引用。使用WeakReference<Bitmap>个对象// OutOfMemoryException

答案 2 :(得分:0)

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

您应该使用Thread或使用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().

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

使用runOnUiThread更新ui并在doInbackground()中执行与netowrk相关的操作。

  runOnUiThread(new Runnable() //run on ui thread
                 {
                  public void run() 
                  { 
                     // update ui
                  }
                 });

答案 3 :(得分: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();