我使用下面的类来下载一个图像,发送的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");
}
url.getContent()不是去访问这个url资源.
URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
finally {
urlConnection.disconnect();
}
}
public static Bitmap downloadBitmap(String url, DefaultHttpClient client){
HttpResponse response = null;
HttpGet get = new HttpGet(url);
try {
response = client.execute(get);
return BitmapFactory.decodeStream(response.getEntity().getContent());
}
catch (ClientProtocolException e1) {
e1.printStackTrace();
}
catch (IllegalStateException e){
e.printStackTrace();
}
catch (IOException e1) {
e1.printStackTrace();
}
return null;
}