android - 将 url 图像设置为 ImageView

标签 android url imageview

<分区>

Possible Duplicate:
Is it possible to use BitmapFactory.decodeFile method to decode a image from http location?

我在将 url 图像设置为 ImageView 时遇到问题。我尝试了以下方法

方法一:

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 图像路径。但是这条路行不通。所以请告诉我哪里出了问题,我会怎么做

提前谢谢你。

最好的问候。

最佳答案

刚刚在我的应用程序中测试了您的“方法 1”,它工作正常。

您可能忘记了 AndroidManifest.xml 文件中的这一行:

<uses-permission android:name="android.permission.INTERNET" />

关于android - 将 url 图像设置为 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4945115/

相关文章:

android - 使用 Glide 将图像加载到 ImageView 中,Uri 从 Intent 返回, Action 为 ACTION_GET_CONTENT

java - 如何在屏幕锁定或 sleep 时暂停和播放媒体播放器?

Perl - 解析 URL 以获取 GET 参数值

url - 如何将我的域名与我的 AWS 应用程序相关联?

Android数据库设置ImageView

android - 拖放时如何找到两个ImageView之间的交集?

android - OnSharedPreferenceChangeListener 未调用 #2

iphone - iPhone 和 Android 上的 GPS...有什么方法可以获取手机的确切位置(不是大概位置)?

android - 不允许在底部应用栏中设置标题

python - 如何使用 `daringfireball` 的正则表达式 re.findall() ?