2012-05-11 24 views
0

我已經把圖像視圖佈局在XML佈局文件如何使用的AsyncTask

<ImageView 
    android:id="@+id/imageData" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10sp" 
    android:layout_marginTop="10sp" 
/> 

,並在我的代碼設置相同的設置存儲爲字符串的URL的形象圖在Android的形象..

ImageView imageData = (ImageView) findViewById(R.id.imageview); 

現在這個爲imageData應該保存圖像時的活動launch..this是我的代碼...

public class TestImageClass extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.imagetable); 
     ImageView mChart = (ImageView) findViewById(R.id.imageview); 
     String testurl = "http://....some image name....jpg"; 
     mChart.setTag(testurl); 
     new DownloadImagesTask().execute(mChart); 


    } 
    public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> { 

     ImageView imageView = null; 

     @Override 
     protected Bitmap doInBackground(ImageView... imageViews) { 
      this.imageView = imageViews[0]; 
      return download_Image((String)imageView.getTag()); 
     } 

     @Override 
     protected void onPostExecute(Bitmap result) { 
      imageView.setImageBitmap(result); 
     } 


     private Bitmap download_Image(String url) { 

      Bitmap bm = null; 
      try { 
       Log.e("inside", "download image....."); 
       URL aURL = new URL(url); 
       URLConnection conn = aURL.openConnection(); 
       conn.connect(); 
       InputStream is = conn.getInputStream(); 
       BufferedInputStream bis = new BufferedInputStream(is); 
       bm = BitmapFactory.decodeStream(bis); 
       bis.close(); 
       is.close(); 
       Log.e("going out of", "download image....."); 
      } catch (IOException e) { 
       Log.e("Hub","Error getting the image from server : " + e.getMessage().toString()); 
      } 

      return bm;   
} 
} 
} 

所以現在如何將這個位圖對象「bm」設置爲我的ImageView?

+0

我用總理對我所有的圖像加載的,它會很容易地處理這個問題。 https://github.com/DHuckaby/Prime – HandlerExploit

+0

如果你有網址爲字符串,那麼你可以檢查這個 http://stackoverflow.com/questions/2471935/how-to-load-an-imageview- by-url-in-android – Ponmalar

回答

0

試試這個,

Drawable d; 
     try { 
      InputStream is = (InputStream) new URL("http://trivetts.iconnectgroup.com/Uploads/Pictures/noname.jpg").getContent(); 
      d = Drawable.createFromStream(is, "src name"); 
      imageView.setBackgroundDrawable(d); 
     } catch (Exception e) { 

     } 
+0

感謝您的回覆,但圖片視圖仍然空白。我需要對xml文件進行任何更改嗎? – mak

+0

你是否遇到任何異常?你是否檢查logcat是否有任何異常被捕獲? 。但仍嘗試在您的清單中提供互聯網許可,<使用權限android:name =「android.permission.INTERNET」/> –

+1

不,我沒有得到任何異常,我已經設置了互聯網權限.. – mak