2012-03-17 4 views
0

私は次のコードを使用して、ローカルホストから画像をダウンロードしています。これは、予期しないエラーが発生し

Runnable r = new Runnable() { 

    @Override 
    public void run() { 

     final String urlString = "http://10.0.2.2/conn/s.png"; 
      // URL newurl = null; 

        try{ 
       do 
       { 
          if (ch==1) 
          { 
           //Thread.sleep(7000); 
           HttpGet httpRequest = new HttpGet(new URL(urlString).toURI()); 
           HttpClient httpClient = new DefaultHttpClient(); 
           HttpResponse response = (HttpResponse) httpClient.execute(httpRequest); 
           HttpEntity entity = response.getEntity(); 
           BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); 
            InputStream is = bufHttpEntity.getContent(); 
            Bitmap bitmap = BitmapFactory.decodeStream(is); 
            im.destroyDrawingCache(); 
            im.setImageBitmap(bitmap); 
            bitmap=null; 
            // Thread.sleep(3000); 
            out.write(1); 
            out.flush(); 




           } 
       } 
       while((ch=in.read()) == 1); 
        } 
        catch(IOException ex) 
        { 

        } 
        catch(URISyntaxException ex) 
        {} 
    } 
}; 

とのスレッドがこのように初期化されている次のコードを実行すると:

    t = new Thread(r); 
      t.start(); 

私は次の例外を取得していますが:

03-18 01:20:50.399: E/AndroidRuntime(305): at android.view.ViewRoot.checkThread(ViewRoot.java:2683) 
03-18 01:20:50.399: E/AndroidRuntime(305): at android.view.ViewRoot.requestLayout(ViewRoot.java:557) 
03-18 01:20:50.399: E/AndroidRuntime(305): at android.view.View.requestLayout(View.java:7918) 
03-18 01:20:50.399: E/AndroidRuntime(305): at android.view.View.requestLayout(View.java:7918) 
03-18 01:20:50.399: E/AndroidRuntime(305): at android.view.View.requestLayout(View.java:7918) 
03-18 01:20:50.399: E/AndroidRuntime(305): at android.view.View.requestLayout(View.java:7918) 
03-18 01:20:50.399: E/AndroidRuntime(305): at android.view.View.requestLayout(View.java:7918) 
03-18 01:20:50.399: E/AndroidRuntime(305): at android.widget.ImageView.setImageDrawable(ImageView.java:306) 
03-18 01:20:50.399: E/AndroidRuntime(305): at android.widget.ImageView.setImageBitmap(ImageView.java:320) 
03-18 01:20:50.399: E/AndroidRuntime(305): at remote.presentation.rshare$1.run(rshare.java:124) 
03-18 01:20:50.399: E/AndroidRuntime(305): at java.lang.Thread.run(Thread.java:1096) 
+0

完全な例外スタックトレースを投稿することはできません。 –

答えて

2

ますUIをスレッドから変更していますが、Androidではこれを行うことはできません。これはUIスレッドで行う必要があります。私はスレッドを使用するAsyncTasksを好む。このAsyncTaskは、URLからイメージを取得します。

クラス

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

    private ImageView view; 

    public GetImage(ImageView iv){ 
     this.view = iv; 
    } 

    protected Bitmap doInBackground(String... params) { 
        //background thread 
     try { 

      return BitmapFactory.decodeStream((InputStream)new URL(params[0]).getContent()); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 

     } catch (IOException e) { 
      e.printStackTrace(); 

     } 

     return null; 

    } 
    @Override 
    protected void onPostExecute(Bitmap b){ 
     if(b != null) 
      this.view.setImageBitmap(b); 
    } 
} 

new GetImage(myImageView).execute(someURL); 

EDIT(編集理由、コメントを参照)、それを使用する - 次のコードの質問アスカーは

を望んでいたとして、繰り返し同じイメージをダウンロードします

GetImageクラスのクラス変数を作成します。

GetImage getImageInstance = new GetImage(myImageView); 
getImageInstance.execute(url); 

は、おそらくだと思う、私はあなたのアプリケーションが何をするかわかりませんが、ユーザーが自分のデータ手当に食べる不要な画像をダウンロードして好きではないだろう

getImageInstance.stopDownloading(); 

をダウンロードを中止するには、クラス

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

    private ImageView view; 
    Bitmap bitmap = null; 
    long lastUpdateTimestamp = System.currentTimeMillis(); 
    public GetImage(ImageView iv){ 
     this.view = iv; 
    } 

    protected Bitmap doInBackground(String... params) { 
        //background thread 
     while(running){ 
      try { 
       //if more than 2 seconds have elapsed update the image 
       if((System.currentTimeMillis() - lastUpdateTimestamp) > 2000){ 
        bitmap = BitmapFactory.decodeStream((InputStream)new URL(params[0]).getContent()); 
        pubilshProgress(bitmap); 
       } 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 

      } catch (IOException e) { 
       e.printStackTrace(); 

      } 
     } 
     return null; 

    } 
    //this method that will update the UI 
    @Override 
    protected void onProgressUpdate(Bitmap... images){ 
     if(images[0] != null) 
      this.view.setImageBitmap(images[0]); 
     lastUpdateTimestamp = System.currentTimeMillis(); 
    } 
    @Override 
    protected void onPostExecute(Void unused){ 
     bitmap.recycle(); 
    } 
    public void stopDownloading(){ 
     running = false; 
    } 
} 

を修正しますあなたがしたいことをするためのより良い方法について。

+0

は、3秒ごとに連続してイメージファイルをストリームする方法を起動します。 –

+0

「イメージをストリーム」という意味が理解できません。 – triggs

+0

2秒ごとにサーバから画像をダウンロードし、画像表示で画面に表示したい –