android - 如何在 Android 上使用 setImageUri()

标签 android uri imagebutton

你能帮帮我吗?我试过了:

ImageButton imgbt=(ImageButton)findViewById(R.id.imgbutton);
Uri imgUri=Uri.parse("/data/data/MYFOLDER/myimage.png");
imgbt.setImageUri(imgUri);

但我什么也没看到,只是一个空按钮。

最佳答案

ImageView.setImageUri 仅适用于本地 Uri,即对本地磁盘文件的引用,而不是网络上图像的 URL。

这是一个如何从网络中获取位图的示例。

    private Bitmap getImageBitmap(String url) {
        Bitmap bm = null;
        try {
            URL aURL = new URL(url);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
       } catch (IOException e) {
           Log.e(TAG, "Error getting bitmap", e);
       }
       return bm;
    } 

从 getImageBitmap() 获得位图后,使用:imgView.setImageBitmap(bm);

关于android - 如何在 Android 上使用 setImageUri(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3870638/

相关文章:

android - 多个屏幕的图像按钮大小

html - 按钮不缩小图像?

android - 将 openStream 与具有带下划线的子域的 URL 一起使用会在 Android 中返回 ioexception。为什么?

Android 实现带拉动刷新的 SwipeListView

android - 未使用 setImageURI() 在 ImageView 中设置图像

c++ - 将 URL 拆分为主机、端口和资源 - C++

android - 如何在不同的 fragment 上使用 ModelView 来获取 EditText 的输入并将 TextView 设置为其值?

android - 如何在 Eclipse 中为 Android 开发配置 Tesseract?

ruby - 带有双哈希 (##) 的 URI 是否无效?

html - ImageButton 和 Input 类型 ="image"边框仅出现在悬停时