我使用下面的類來下載一個圖像,發送的url是正確的,但是url.getContext() 每次都返回null。問題出在哪裡呢?
package com.WasserSportLotse;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.content.Context;
import android.graphics.drawable.Drawable;
class ImageDownloader {
private Drawable d;
public ImageDownloader(Context ctx, String url, String saveFilename) {
try {
InputStream is = (InputStream) this.fetch(url);
d = Drawable.createFromStream(is, "src");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public Drawable getImage(){
return d;
}
public Object fetch(String address) throws MalformedURLException,IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
}
private String path = "http://www.*YOURPATH.COM/SOMETHING*";
private String file;
private String url;
public ImageDownloader(String file){
this.file = URLEncoder.encode(file);
url = path + this.file;
}
public Drawable getImage() throws IOException, MalformedURLException {
return Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).getContent()), "name");
}