显示图片(从URL)在ImageView的 [英] Display Image(From Url) In ImageView
本文介绍了显示图片(从URL)在ImageView的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用这code来显示URL的图像。我的code运行时,并没有给出一个错误,但不显示图像。 任何帮助?
下面是code:
公共类MainActivity延伸活动{
ImageView的我;
字符串IMAGEURL =http://64.250.238.26:1111/clips/sunsetsofmauisplash.jpg;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
尝试 {
I =(ImageView的)findViewById(R.id.image);
点阵位图= BitmapFactory.de codeStream((InputStream的)新的URL(IMAGEURL).getContent());
i.setImageBitmap(位);
}赶上(MalformedURLException异常E){
}赶上(IOException异常E){
}
}
}
解决方案
试试这个方法,并记录code会告诉你,如果有被抛出的异常
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
尝试 {
网址URL =新的URL(IMAGEURL);
HTTPGET HTT prequest = NULL;
HTT prequest =新HTTPGET(url.toURI());
HttpClient的HttpClient的=新DefaultHttpClient();
HTT presponse响应=(HTT presponse)httpclient.execute(HTT prequest);
HttpEntity实体= response.getEntity();
BufferedHttpEntity b_entity =新BufferedHttpEntity(实体);
输入的InputStream = b_entity.getContent();
点阵位图= BitmapFactory.de codeStream(输入);
ImageView的I =(ImageView的)findViewById(R.id.image);
i.setImageBitmap(位);
}赶上(MalformedURLException异常E){
Log.e(日志,坏网址,T);
}赶上(IOException异常E){
Log.e(日志,IO错误,T);
}
}
更新:
在挖我发现这个修复到去$ C $当时正在记录CR误差
I am using this code to display an image from a URL. My code is run and it does not give an error but the image is not displayed. Any help?
Here is the code:
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) {
}
}
}
解决方案
try this approach and the logging code will show you if there are any exceptions being thrown
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);
}
}
Update:
After digging I found this Fix to the decoder error that was being logged
这篇关于显示图片(从URL)在ImageView的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文