我创建了一个Android应用程序,其中图像从给定的url加载。当我直接传递url时,图像正在加载。但是当我在字符串变量中获取url并传递该变量时,图像不会加载。我的代码如下。
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 newUrl=realImageUrl.replace("https", "http");
InputStream is = (InputStream) this.fetch(newUrl);
Log.e("https,SubString http: ",realImageUrl+","+ a);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
return null;
} catch (IOException e) {
return null;
}
}
public Object fetch(String address) throws MalformedURLException,
IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
此代码无效。我的新网址是newUrl。当我在我的日志中打印newUrl并直接给出该url而不是newUrl时,图像正在加载。
答案 0 :(得分:3)
我找到了解决方案。我已经更新了fetch(String address)方法。
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;
}