使用带参数的网址从互联网上下载Android图片
此代码有效,但我不希望这样,我想使用带参数的网址下载图片。(例如:网址= 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 :(得分: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
。