首页 > 解决方案 > 如何在片段中添加网址图像?

问题描述

我想在片段中添加 url 图像,但我无法做到这一点。

   URL imageUrl = new URL("http://image10.bizrate-images.com/resize?sq=60&uid=2216744464");

public HomeFragment() throws MalformedURLException {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_home, container, false);

    try {
        ImageView i = (ImageView)rootView.findViewById(R.id.image);
        Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
        i.setImageBitmap(bitmap);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return rootView;
}

它显示错误

错误:(48, 77) 错误:不兼容的类型:无法将 URL 转换为字符串

错误:任务“:app:compileDebugJavaWithJavac”执行失败。

编译失败;有关详细信息,请参阅编译器错误输出。

标签: android

解决方案


您可以使用Picasso从 url 加载图像。

Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView);

或使用滑翔

Glide.with(fragment).load(myUrl).into(imageView);

推荐阅读