当我从openhttpconnection获取一个获取jpeg文件url的输入流时,我总是得到一个返回null的位图。例如,我的网址字符串是
http://10.0.2.2:8000/mydirectory/myfile.jpg
其中8000是使用模拟器运行的本地服务器的端口。我可以使用相同类型的URL来从web服务获取,但不能获取图像文件。有什么不同我要做一个jpgeg文件到位图或我的网址格式有什么问题吗?以下是我的代码。任何帮助表示赞赏。
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;
}