1

使用带参数的 URL从 Internet 下载 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;
}
4

1 回答 1

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();转换DrawableBitMap

于 2013-03-01T06:13:48.727 回答