Android haw将base64的映像从数据库插入到recycleView

Android haw将base64的映像从数据库插入到recycleView,android,Android,我正在构建一个获取API信息的循环视图,它工作得很好,但直到现在我使用了可绘制文件夹中的图像,但我需要使用API中的base64(格式字符串)图像对其进行更改 然后,伙计们如何更改我的代码,使回收得到API图像 NewsItem.class public class NewsItem { String Title,Content,Date,type_col; int userPhoto; public NewsItem(){

我正在构建一个获取API信息的循环视图,它工作得很好,但直到现在我使用了可绘制文件夹中的图像,但我需要使用API中的base64(格式字符串)图像对其进行更改

然后,伙计们如何更改我的代码,使回收得到API图像

NewsItem.class

    public class NewsItem {

        String Title,Content,Date,type_col;
        int userPhoto;
        public  NewsItem(){

        }

        public NewsItem(String title, String content, String date, String type_col, int userPhoto) {
            Title = title;
            Content = content;
            Date = date;
            this.type_col = type_col;
            this.userPhoto = userPhoto;
        }

        public String getType_col() {
            return type_col;
        }

        public void setType_col(String type_col) {
            this.type_col = type_col;
        }

        public void setTitle(String title) {
            Title = title;
        }

        public void setContent(String content) {
            Content = content;
        }

        public void setDate(String date) {
            Date = date;
        }

        public void setUserPhoto(int userPhoto) {
            this.userPhoto = userPhoto;
        }

        public String getTitle() {
            return Title;
        }

        public String getContent() {
            return Content;
        }

        public String getDate() {
            return Date;
        }

        public int getUserPhoto() {
            return userPhoto;
        }
    }
新闻适配器:

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.NewsViewHolder> {

    Context mContext;
    List<NewsItem> mData;

    public NewsAdapter(Context mContext, List<NewsItem> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @NonNull
    @Override
    public NewsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View layout = LayoutInflater.from(mContext).inflate(R.layout.item_news,parent,false);

        return new NewsViewHolder(layout);
    }

    @Override
    public void onBindViewHolder(@NonNull NewsViewHolder newsViewHolder, int position) {
        //bind data
        newsViewHolder.tv_title.setText(mData.get(position).getTitle());
        newsViewHolder.tv_description.setText(mData.get(position).getContent());
        newsViewHolder.tv_date.setText(mData.get(position).getDate());
        newsViewHolder.tv_type_col.setText(mData.get(position).getType_col());
        newsViewHolder.img_user.setImageResource(mData.get(position).getUserPhoto());

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class NewsViewHolder extends RecyclerView.ViewHolder{

        TextView tv_title,tv_description,tv_date,tv_type_col;
        ImageView img_user;

        public NewsViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_description = itemView.findViewById(R.id.tv_description);
            tv_date = itemView.findViewById(R.id.tv_date);
            tv_type_col = itemView.findViewById(R.id.tv_type_col);
            img_user = itemView.findViewById(R.id.img_user);
        }
    }
}
公共类NewsAdapter扩展了RecyclerView.Adapter{ 语境; 列出mData; 公共新闻适配器(上下文mContext、列表mData){ this.mContext=mContext; this.mData=mData; } @非空 @凌驾 public NewsViewHolder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){ 视图布局=LayoutFlater.from(mContext)。充气(R.layout.item\u news,parent,false); 返回新的新闻视图持有者(布局); } @凌驾 public void onBindViewHolder(@NonNull NewsViewHolder NewsViewHolder,int位置){ //绑定数据 newsViewHolder.tv_title.setText(mData.get(position.getTitle()); newsViewHolder.tv_description.setText(mData.get(position.getContent()); newsViewHolder.tv_date.setText(mData.get(position.getDate()); newsViewHolder.tv_type_col.setText(mData.get(position.getType_col()); newsViewHolder.img_user.setImageResource(mData.get(position.getUserPhoto()); } @凌驾 public int getItemCount(){ 返回mData.size(); } 公共类NewsViewHolder扩展了RecyclerView.ViewHolder{ text查看电视标题、电视描述、电视日期、电视类型颜色; ImageView img_用户; 公共新闻视图持有者(@NonNull View itemView){ 超级(项目视图); tv_title=itemView.findviewbyd(R.id.tv_title); tv_description=itemView.findviewbyd(R.id.tv_description); tv_date=itemView.findviewbyd(R.id.tv_date); tv\u type\u col=itemView.findviewbyd(R.id.tv\u type\u col); img_user=itemView.findviewbyd(R.id.img_user); } } } 主要片段:

RecyclerView NewsRecyclerview;
    NewsAdapter newsAdapter;
    List<NewsItem> mData;


 public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

NewsRecyclerview = root.findViewById(R.id.news_rv);
        mData = new ArrayList<>();

        getd("all");}

    private void getd(String selector){//, final GalleryFragment.MarkerLoadCallBack markerLoadCallBack
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Api.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
                .build();

        Api api = retrofit.create(Api.class);

        Call<List<col>> call = api.getHeroes(selector);

        call.enqueue(new Callback<List<col>>() {
            @Override
            public void onResponse(Call<List<col>> call, Response<List<col>> response) {
                // mker = new ArrayList<>();
                List<col> colList = response.body();
                for (col c : colList) {
                    Log.d("image : ", c.getImagePath() + "");

                    mData.add(new NewsItem(c.getNom_col().toString(),"L'email de Client est:" + c.getEmailcol()+"\n" +  "La matricule est :     \n"+ "Créer par "+ c.getCreator() + "\n"  ,"le "+c.getDate_creation_col().toString(),"Type:" + c.getType(),R.drawable.t));
                }
                newsAdapter = new NewsAdapter(getContext(),mData);
                NewsRecyclerview.setAdapter(newsAdapter);
                NewsRecyclerview.setLayoutManager(new LinearLayoutManager(getContext()));

            }
RecyclerView新闻RecyclerView;
新闻适配器;
列出mData;
公共视图onCreateView(@NonNull layoutiner充气机,
视图组容器,捆绑包savedInstanceState){
NewsRecyclerview=root.findViewById(R.id.news\u rv);
mData=newarraylist();
getd(“全部”);}
私有void getd(字符串选择器){/,final GalleryFragment.MarkerLoadCallBack MarkerLoadCallBack
改装改装=新改装.Builder()
.baseUrl(Api.BASE\u URL)
.addConverterFactory(GsonConverterFactory.create())//这里我们使用GsonConverterFactory将json数据直接转换为对象
.build();
Api=改装.create(Api.class);
Call Call=api.getHeroes(选择器);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
//mker=newarraylist();
List colList=response.body();
对于(c列:colList){
Log.d(“image:,c.getImagePath()+”);
MeDATSIM(C.GETNOMILCORE)(toSTRIN),“Le'Email Eclipse EST:“+C.GeMeMeCyCe(+)”\n“+”La预科EST:\n“+”Cr r er-PAR“+C.GeCaltoCuffe”+“\n”,“LE”+C.GATDATEYCREATIONION CORE(),TySTRIN(),“类型:+C.GETType(),R.Dabel.t);
}
newsAdapter=新的newsAdapter(getContext(),mData);
设置适配器(newsAdapter);
setLayoutManager(新的LinearLayoutManager(getContext());
}

字符串形式的图像是c.getImagePath()

滑动库支持base 64格式。因此,您可以使用此库通过路径绑定图像。有关详细信息,请参阅下面的链接。

滑动库支持base 64格式。因此,您可以使用此库通过路径绑定图像。有关详细信息,请参阅下面的链接
c.getImagePath()
包含base64编码的字符串,这不是我正在等待的答案。到底是什么让base64编码的?如果它是一个指向映像的路径,那么我认为您不会让base64编码该路径。因此,此语句记录了什么:
Log.d(“映像:,c.getImagePath()+”)
?不是我使用的路径`col_photo.setImageBitmap(位图)``ByteArrayOutputStream ByteArrayOutputStream=new ByteArrayOutputStream();`bitmap.compress(bitmap.CompressFormat.PNG,100,ByteArrayOutputStream)``byteArray=ByteArrayOutputStream.toByteArray();String encoded=Base64.encodeToString(byteArray,Base64.DEFAULT);`before将图像更改为字符串,以便imagepath是字符串表单上的真实图像Base64编码字符串中的确切内容?
c.getImagePath()
contans base64编码的字符串,这不是我正在等待的答案。到底是什么编码了base64?如果它是一个指向映像的路径,那么我认为您不会让base64编码该路径。因此,此语句记录了什么:
Log.d(“映像:,c.getImagePath()+”)
?不是我使用的路径`col_photo.setImageBitmap(位图)``ByteArrayOutputStream ByteArrayOutputStream=new ByteArrayOutputStream();`bitmap.compress(bitmap.CompressFormat.PNG,100,ByteArrayOutputStream)``byteArray=ByteArrayOutputStream.toByteArray();String encoded=Base64.encodeToString(byteArray,Base64.DEFAULT);`before用于将图像更改为字符串,以便imagepath是字符串表单上的真实图像