我正在使用此代码显示网址中的图片。我的代码运行,它不会出错,但不显示图像。 有什么帮助吗?
以下是代码:
public class MainActivity extends Activity {
ImageView i;
String imageUrl = "http://64.250.238.26:1111/clips/sunsetsofmauisplash.jpg";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
i = (ImageView) findViewById(R.id.image);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(imageUrl).getContent());
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
} catch (IOException e) {
}
}
}
答案 0 :(得分:3)
尝试这种方法,日志代码将显示是否抛出任何异常
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
URL url = new URL(imageUrl);
HttpGet httpRequest = null;
httpRequest = new HttpGet(url.toURI());
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
InputStream input = b_entity.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(input);
ImageView i = (ImageView) findViewById(R.id.image);
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
Log.e("log", "bad url", t);
} catch (IOException e) {
Log.e("log", "io error", t);
}
}
更新
挖掘后,我发现这个Fix到了正在记录的解码器错误