Get favicon from a web and save it to declared Icon in a Pojo class

后端 未结 2 1823
北海茫月
北海茫月 2020-12-12 08:32

I have a RecyclerViewAdapter which I have called MyAdapter and there I set for a Bookmark: ID, Name, Icon, SearchUrl.

I am able to show all

相关标签:
2条回答
  • 2020-12-12 08:48

    Why are you using setText? This should work

    URL url = new URL(arrayList.get(position).getSearchUrl());
    Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    imageView.setImageBitmap(bmp);
    
    0 讨论(0)
  • 2020-12-12 08:56

    You can use besticon

    This is a favicon service:

    • Supports favicon.ico and apple-touch-icon.png

    • Simple URL API

    • Fallback icon generation

    • Docker image & single binary download for easy hosting

    SAMPLE CODE HOW To use it

    Layout

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/parentView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="vertical">
    
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
    
            <ImageView
                android:id="@+id/stackIMG"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_margin="10dp" />
    
            <ImageView
                android:id="@+id/fbIMG"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_margin="10dp" />
    
            <ImageView
                android:id="@+id/twitterIMG"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_margin="10dp" />
    
        </LinearLayout>
    
    
    </LinearLayout>
    

    Activity code

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.ImageView;
    
    import com.bumptech.glide.Glide;
    import com.bumptech.glide.request.RequestOptions;
    
    public class MainActivity extends AppCompatActivity {
    
    
        String stackOverFlowURL, FacebookURL, TwitterURL;
        String BASE_URL = "https://besticon-demo.herokuapp.com/icon?url=";
    
    
        ImageView stackIMG,fbIMG,twitterIMG;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            stackIMG = findViewById(R.id.stackIMG);
            fbIMG = findViewById(R.id.fbIMG);
            twitterIMG = findViewById(R.id.twitterIMG);
    
    
            stackOverFlowURL = BASE_URL + "https://stackoverflow.com/" + "&size=32;";
            FacebookURL = BASE_URL + "https://www.facebook.com/" + "&size=32;";
            TwitterURL = BASE_URL + "https://twitter.com" + "&size=32;";
    
            RequestOptions requestOptions = new RequestOptions();
            requestOptions.error(R.drawable.ic_fav);
    
            Glide.with(MainActivity.this)
                    .load(stackOverFlowURL)
                    .apply(requestOptions)
                    .into(stackIMG);
    
            Glide.with(MainActivity.this)
                    .load(FacebookURL)
                    .apply(requestOptions)
                    .into(fbIMG);
    
    
            Glide.with(MainActivity.this)
                    .load(TwitterURL)
                    .apply(requestOptions)
                    .into(twitterIMG);
        }
    
    
    }
    

    OUTPUT

    UPDATE

    make below changes in your code

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        String BASE_URL = "https://besticon-demo.herokuapp.com/icon?url=";
    
        final int itemType = getItemViewType(position);
        if (itemType == ITEM_TYPE_ONE) {
            final ViewHolder viewHolder = (ViewHolder) holder;
    
            viewHolder.tvName.setText(arrayList.get(position).getName());
    
            Glide.with(context)
                    .load(BASE_URL+arrayList.get(position).getSearchUrl()+"&size=32;")
                    .apply(requestOptions)
                    .into(viewHolder.tvIcon);
    
            viewHolder.tvId.setText(arrayList.get(position).getId());
            viewHolder.tvSearchUrl.setText(arrayList.get(position).getSearchUrl());
            viewHolder.tvNativeUrl.setText(arrayList.get(position).getNativeUrl());
            viewHolder.tvIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent;
                    String Url = viewHolder.tvSearchUrl.getText().toString();
                    String Name = viewHolder.tvName.getText().toString();
                    intent = new Intent(context, BookmarkActivity.class);
                    intent.putExtra("WebSearchUrl", Url);
                    intent.putExtra("WebSearchName", Name);
                    v.getContext().startActivity(intent);
                }
            });
    
    
            viewHolder.tvIcon.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    Intent intent = new Intent(context, ActivityChangeBookmark.class);
                    String Url = viewHolder.tvSearchUrl.getText().toString();
                    String Name = viewHolder.tvName.getText().toString();
                    Integer Id = viewHolder.tvId.getId();
                    Bundle extras = new Bundle();
                    viewHolder.tvIcon.buildDrawingCache();
                    Bitmap image = viewHolder.tvIcon.getDrawingCache();
                    intent.putExtra("Url", Url);
                    intent.putExtra("Name", Name);
                    intent.putExtra("ID", Id);
                    extras.putParcelable("Image", image);
                    intent.putExtras(extras);
                    v.getContext().startActivity(intent);
                    return false;
                }
            });
    
        } else if (itemType == ITEM_TYPE_TWO) {
            ButtonViewHolder buttonViewHolder = (ButtonViewHolder) holder;
            buttonViewHolder.imgButton.setImageResource(arrayList.get(position).getIcon());
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题