2013-03-01 79 views
1

使用帶參數的URL從Android的互聯網下載圖像??? ???使用帶參數的url從Android上下載圖像?

該代碼的工作,但我不希望出現這種情況,我想用一個URL參數下載圖像(例如:URL = http://yahoo.com/record?ProductID=1)。

如何改變這種代碼嗎?。

bmp = this.GetNetBitmap("http://avatar.csdn.net/B/D/5/1_redoffice.jpg"); 

public Bitmap GetNetBitmap(String url){ 
URL imageUrl = null; 
Bitmap bitmap = null; 
try{ 
imageUrl = new URL(url); 
} 
catch(MalformedURLException e){ 
Log.e("DownloadTimerTask", e.getMessage()); 
} 
try{ HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection(); 
conn.setDoInput(true); 
conn.connect(); 
InputStream is = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(is); is.close(); 
} 
catch(IOException e){ 
Log.e("DownloadTimerTask", e.getMessage()); 
} 
return bitmap; 
} 

回答

0

寫一個函數是類似的路線如下:

public static Drawable imageFromURL(String url) { 
    try { 
     InputStream is = (InputStream) new URL(url).getContent(); 
     Drawable d = Drawable.createFromStream(is, "src name"); 
     return d; 
    } catch (Exception e) { 
     return null; 
    } 
} 

然後使用BitmapFactory.decodeResource();Drawable轉換爲BitMap,如果你的願望。