为什么图像没有从远程服务器加载到我的android应用程序中?
我使用了两种方法从远程服务器加载映像。它在2.1版(三星)和2.3.4版(索尼爱立信Xperia Neo V)中运行良好。但在三星移动V-2.2中,该方法返回空值。代码如下所示为什么图像没有从远程服务器加载到我的android应用程序中?,android,web-services,url,uri,image-loading,Android,Web Services,Url,Uri,Image Loading,我使用了两种方法从远程服务器加载映像。它在2.1版(三星)和2.3.4版(索尼爱立信Xperia Neo V)中运行良好。但在三星移动V-2.2中,该方法返回空值。代码如下所示 private Drawable ImageOperations(String url, String saveFilename) { try { String realImageUrl = url + "?email=" + Constants.email + "
private Drawable ImageOperations(String url, String saveFilename) {
try {
String realImageUrl = url + "?email=" + Constants.email
+ "&proc_date=" + Constants.proc_date + "&access_key="
+ Constants.ACCESS_KEY + "&version=1.00";
String a = realImageUrl.replace("https", "http");
InputStream is = (InputStream) this.fetch(a);
Log.e("https,SubString http: ", realImageUrl + "," + a);
Drawable d = Drawable.createFromStream(is, "saveFilename");
return d;
} catch (MalformedURLException e) {
return null;
} catch (IOException e) {
return null;
}
}
public Object fetch(String address) throws MalformedURLException,
IOException {
try {
URL url = new URL(address);
URI uri = new URI(url.getProtocol(), url.getUserInfo(),
url.getHost(), url.getPort(), url.getPath(),
url.getQuery(), url.getRef());
url = uri.toURL();
Log.i("Url:", url + "");
Object content = url.getContent();
return content;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
您可能希望在错误陷阱中打印堆栈跟踪。它们只返回null,但不告诉您捕获了什么错误。实际上,它没有显示任何错误或异常。当我使用web浏览器点击url时,有一个图像,但是,对于android 2.2,这段代码返回null。请尝试在getContent方法之前添加这三行,并重构getContent方法:URLConnection;connection=url.openConnection();connection.connect();对象内容=connection.getContent();