gpt4 book ai didi

android - 在 ImageView 中显示图像(来自 Url)

转载 作者:搜寻专家 更新时间:2023-11-01 09:16:57 24 4
gpt4 key购买 nike

我正在使用此代码显示来自 URL 的图像。我的代码运行了,它没有给出错误,但是没有显示图像。有帮助吗?

代码如下:

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) {

}

}
}

最佳答案

试试这个方法,日志代码会告诉你是否有任何异常被抛出

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到正在记录的解码器错误

关于android - 在 ImageView 中显示图像(来自 Url),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3996702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com