当我从获取 jpeg 文件 url 的 openhttpconnection 获取输入流时,我总是得到 null 的位图返回。例如,我的 url 字符串是
http://10.0.2.2:8000/mydirectory/myfile.jpg
其中 8000 是使用模拟器运行的本地服务器的端口。我可以使用这种相同类型的 url 从 web 服务获取,但不能用于获取图像文件。要将 jpgeg 文件制作成位图,我需要做些什么不同的事情,还是我的 url 格式有问题?下面是我的代码。任何帮助表示赞赏。
private Bitmap downloadImage(String url) {
Bitmap bmap = null;
InputStream inStream = null;
try {
inStream = openHttpConnection(url);
Log.d("inStream", String.valueOf(inStream));
bmap = BitmapFactory.decodeStream(inStream);
if(inStream != null)
inStream.close();
}
catch (IOException el) {
el.printStackTrace();
}
return bmap;
}
private InputStream openHttpConnection(String urlString) throws IOException {
InputStream inStream = null;
int checkConn = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
checkConn = httpConn.getResponseCode();
if (checkConn == HttpURLConnection.HTTP_OK) {
inStream = httpConn.getInputStream();
}
}
catch (Exception ex) {
throw new IOException("Error connecting");
}
return inStream;
}