简体   繁体   English

如何从URL在imageView中显示图像

[英]how to show image in imageView from url

I have a gridview which includes some images, when I click on one of them it comes up with some text but not the image itself as I want the image to load from a url. 我有一个包含一些图像的gridview,当我单击其中的一个时,它会显示一些文本,但不是图像本身,因为我想从URL加载图像。 I'm using picasso to load the images as I found this the easiest approach, please advise what am I doing wrong 我正在使用毕加索来加载图像,因为我发现这是最简单的方法,请告知我做错了什么

smiles_items_layout.xml smiles_items_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView android:id="@+id/smile_image_view"
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:layout_gravity="center"/>
</LinearLayout>

BottomSheetDialog_Smiles.java BottomSheetDialog_Smiles.java

@Override
    public View getView(final int position, final View convertView,
                        ViewGroup parent) {

        final Holder holder = new Holder();
        LayoutInflater inflater = (LayoutInflater)

getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        grid = inflater.inflate(R.layout.smiles_items_layout, null);

        holder.img = (ImageView) grid.findViewById(R.id.smile_image_view);
        holder.img.setImageResource(mThumbIds[position]);

        gridView2.setOnItemClickListener(new 
AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, 
                   int i, long l) {

               JSONDictionary imageChat = new JSONDictionary();
               Picasso.with(context)
                        .load("https://wallpapercave.com/wp/JUwBXRw.jpg")
                        .resize(50,50).into(holder.img);
                imageChat.put("message", holder.img);
                Communicator.getInstance().emit("new chat message", 
                imageChat);

            }
        });

        return grid;

    }
}

This is the result that displays 这是显示的结果

这是显示的结果

I have a gridview which includes some images, when I click on one of them it comes up with some text but not the image itself as I want the image to load from a url. 我有一个包含一些图像的gridview,当我单击其中的一个时,它会显示一些文本,但不是图像本身,因为我想从URL加载图像。

=> Reason behind you are seeing object address is because you are passing ImageView in chat message. =>之所以看到对象地址,是因为您在聊天消息中传递了ImageView。

imageChat.put("message", holder.img);

Rather you should just pass an image address like: 相反,您应该只传递一个图像地址,例如:

imageChat.put("image_to_load", "https://wallpapercave.com/wp/JUwBXRw.jpg");

and on receiver side, write a code to load image using same code: 在接收方,编写一个代码以使用相同的代码加载图像:

Picasso.with(context)
       .load("https://wallpapercave.com/wp/JUwBXRw.jpg")
       .resize(50,50).into(holder.img);

Because Picasso helps you in loading images asynchronously from given URLs, so you should make a call when it's actually required. 由于Picasso可以帮助您从给定的URL异步加载图像,因此您应该在实际需要时进行呼叫。 In your case, images should be loaded on receiver side. 在您的情况下,图像应加载到接收方。

In my app I do something like that: 在我的应用程序中,我执行以下操作:

Glide.with(getActivity()).load(url)
     .diskCacheStrategy(DiskCacheStrategy.SOURCE)
     .centerCrop().into(imageview);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM