set url image to image view(将 url 图像设置为图像视图)
问题描述
可能重复:
是否可以使用 BitmapFactory.decodeFile 方法从 http 位置解码图像?
我在将 url 图像设置为图像视图时遇到问题.我尝试了以下方法
i have a problem in set url image to image view. i tried below methods
方法一:
Bitmap bimage= getBitmapFromURL(bannerpath);
image.setImageBitmap(bimage);
public static Bitmap getBitmapFromURL(String src) {
try {
Log.e("src",src);
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
Log.e("Bitmap","returned");
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
Log.e("Exception",e.getMessage());
return null;
}
}
方法二:
Drawable drawable = LoadImageFromWebOperations(bannerpath);
image.setImageDrawable(drawable);
private Drawable LoadImageFromWebOperations(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
我尝试了以上两种方法,但都不起作用.两者都显示 DEBUG/skia(266): --- decoder->decode returned false .我使用了有效的演示 url 图像路径.但这条路行不通.所以请告诉我出了什么问题,我会怎么做
i tried above two methods but are not working . both are showing DEBUG/skia(266): --- decoder->decode returned false . i used demo url image paths that is working. but this path is not working .so please tell me what is the wrong and what i will do
提前谢谢你.
最好的问候.
推荐答案
刚刚在我的应用程序中测试了您的方法 1",它工作得很好.
Just Tested your "Method 1" in my app and it works just fine.
您可能在 AndroidManifest.xml 文件中忘记了这一行:
You might have forgotten this line in your AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET" />
这篇关于将 url 图像设置为图像视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 url 图像设置为图像视图
基础教程推荐
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
