Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

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

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 of them but for the icon it works only from Drawable. I don't want to convert Bitmap to Drawable I only want take favicon of url which is saved in DB.
I want to take the favicon from the Bitmap because the icon depens from which SearchUrl it is gaven for example.
SearchUrl = "https://www.youtube.com" In the icon I need to show the favicon of the youtube. As I said manually this works from drawable but this is depended from the user which I do not know what kind of Url it is written.

Below you can find the code.

@Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final int itemType = getItemViewType(position);
        if (itemType == ITEM_TYPE_ONE) {
            final ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.tvName.setText(arrayList.get(position).getName());

            Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(viewHolder.tvSearchUrl.setText(arrayList.get(position).getSearchUrl())).getContent()); //Here is the error of the new URL
            viewHolder.tvIcon.setImageBitmap(bitmap);
            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());
        }

    }

If I change viewHolder.tvIcon.setImageResource(arrayList.get(position).getIcon()); than this takes from the drawable and workd only for the icons which are in drawable.

Bookmark.class

public class Bookmark {
    String name, id, nativeUrl, searchUrl;
    int icon;
    int viewType;


    public String getName() { return name; }
    public void setName(String name) {
        this.name = name;
    }

    public int getIcon() { return icon; }
    public void setIcon(int icon) {
        this.icon = icon;
    }

    public String getId(){
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getNativeUrl() {
        return nativeUrl;
    }

    public void setNativeUrl(String nativeUrl) {
        this.nativeUrl = nativeUrl;
    }

    public String getSearchUrl() {
        return searchUrl;
    }

    public void setSearchUrl(String searchUrl) {
        this.searchUrl = searchUrl;
    }
    public int getViewType() {
        return viewType;
    }

    public void setViewType(int viewType) {
        this.viewType = viewType;
    }


    @Override
    public String toString() {
        return "Bookmark{" +
                "name='" + name + ''' +
                ", icon='" + icon + ''' +
                ", id='" + id + ''' +
                ", nativeUrl='" + nativeUrl + ''' +
                ", searchUrl='" + searchUrl + ''' +
                '}';
    }
}

ActivityChangeBookmark.class

 public class ActivityChangeBookmark  extends AppCompatActivity {
 private ArrayList<String> listItems = new ArrayList<String>();
 private RecyclerView recyclerView;
 Button saveBookmark, cancelBookmark;
 TextView name, url;
 ImageView icon, mIcon;
 EditText mName, mUrl, mID;
 public static final String Save_Bookmark = "Save_Bookmark";
 String Url, Name;
 Integer Id;
 MyAdapter myAdapter;
 BookmarkDB bookmarkDB;
 ButtonRobotoMedium removeBookmark, sendToHomeScreen;
 ArrayList<Bookmark> arrayList = new ArrayList<>();


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_change_bookmark);
    saveBookmark = findViewById(R.id.btnSave);
    name = findViewById(R.id.tvNameEdit);
    url = findViewById(R.id.tvURLEdit);
    recyclerView = findViewById(R.id.myRecyclerView);
    icon = findViewById(R.id.ivFavIcon);
    mName = findViewById(R.id.etNameEdit);
    mUrl = findViewById(R.id.etURLEdit);
    sendToHomeScreen = findViewById(R.id.btnAddIconToDeviceScreen);
    name.setVisibility(View.GONE);
    url.setVisibility(View.GONE);
    bookmarkDB = new BookmarkDB(getApplicationContext());
    Intent intent = getIntent();
    myAdapter = new MyAdapter(getApplicationContext(), arrayList);

    Bundle extras = intent.getExtras();
    if (extras != null) {
        Url = intent.getExtras().getString("Url");
        Name = intent.getExtras().getString("Name");
        Id = intent.getExtras().getInt("ID");

        Bitmap bitmap = intent.getExtras().getParcelable("Image");
        mName.setText(Name);
        mUrl.setText(Url);

        icon.setImageBitmap(bitmap);
        removeBookmark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bookmarkDB.deleteName(Id, Name);
                myAdapter.notifyDataSetChanged();
                finish();
            }
        });
    }
     else {
        removeBookmark.setVisibility(View.GONE);
        sendToHomeScreen.setVisibility(View.GONE);
        icon.setImageResource(R.drawable.user_bookmark);
        mName.setText("");
        mUrl.setText("");
    }


    saveBookmark.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            saveData();
            myAdapter.notifyDataSetChanged();
            finish();
            }
    });

public void saveData() {
    Random r = new Random();
    int low = 14;
    int high = 100;
    int result = r.nextInt(high-low) + low;
    bookmarkDB.addBookmark(result, mName.getText().toString(), false, "https://www.google.com/s2/favicons?domain=" + mUrl.getText().toString(), mUrl.getText().toString(), "http://" + mUrl.getText().toString());
 }
}

BookmarkDB.class

 public class BookmarkDB extends SQLiteOpenHelper {
    public static final String DBNAME = "bookmarks.db"; // The name of the 
 database file
    public static final int DBVERSION = 1;  // The Database version

    public static final String TBL_BOOKMARK = "bookmark";
    public static final String COL_ID = BaseColumns._ID; // equates to _id
    public static final String COl_NAME = "name";
    public static final String COl_HIDDEN = "hidden";
    public static final String COL_ICON = "icon";
    public static final String COL_NATIVEURL = "nativeurl";
    public static final String COL_SEARCHURL = "searchurl";

    SQLiteDatabase mDB;

    public BookmarkDB(Context context) {
        super(context, DBNAME, null, DBVERSION);
        mDB = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        // The SQL to be used to create the table
        String crt_bookmark_tbl_sql = "CREATE TABLE IF NOT EXISTS " + TBL_BOOKMARK + "(" +
                COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COl_NAME + " TEXT, " +
                COl_HIDDEN + " INTEGER, " +
                COL_ICON + " TEXT, " +
                COL_NATIVEURL + " TEXT," +
                COL_SEARCHURL + " TEXT" +
                ")";
        db.execSQL(crt_bookmark_tbl_sql); // CREATE THE TABLE

    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP IF TABLE EXISTS " + DBNAME);
        onCreate(db);
    }

    public long addBookmark(long id, String name, boolean hidden, String icon, String nativeurl, String searchurl) {
        ContentValues cv = new ContentValues();

        cv.put(COl_NAME,name);
        cv.put(COl_HIDDEN,hidden);
        cv.put(COL_ICON,icon);
        cv.put(COL_NATIVEURL,nativeurl);
        cv.put(COL_SEARCHURL,searchurl);
        // uses the convenience insert method that builds the SQL
        return mDB.insert(TBL_BOOKMARK,null,cv);
    }
    public ArrayList<Bookmark> getAllBookmarks() {
        ArrayList<Bookmark> rv  = new ArrayList<>();
        Cursor csr = mDB.query(TBL_BOOKMARK,null,null,null,null,null, null);
        while (csr.moveToNext()) {
            Bookmark b = new Bookmark();
            b.setId(csr.getString(csr.getColumnIndex(COL_ID)));
            b.setName(csr.getString(csr.getColumnIndex(COl_NAME)));
            b.setIcon(csr.getInt(csr.getColumnIndex(COL_ICON))); // Here I set the icon but I want to set icon from the saved SearchUrl.
            b.setViewType(csr.getInt(csr.getColumnIndex(COl_NAME)));
            b.setNativeUrl(csr.getString(csr.getColumnIndex(COL_NATIVEURL)));
            b.setSearchUrl(csr.getString(csr.getColumnIndex(COL_SEARCHURL)));
            rv.add(b);
        }
        return rv;
    }

FragmentBookmark.class

public class FragmentBookmark extends Fragment {
    ArrayList<Bookmark> arrayList = new ArrayList<>();
    MyAdapter myAdapter;
    View paramView;
    RecyclerView myRecyclerView;
    private Context mContext;
    BookmarkDB mDB;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
        mDB = new BookmarkDB(mContext);
        mDB.getAllBookmarks();
        buildBookmarkArrayListfromDB();
        loadBookMarksFromXML();
        mDB.logAllBookmarkRows();
    }
    @Nullable
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        paramView = inflater.inflate(R.layout.bookmark, container, false);
        myRecyclerView =  paramView.findViewById(R.id.myRecyclerView);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

enter image description here

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());
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...