将图像加载到android的最佳方法

时间:2017-03-18 10:33:24

标签: php android android-recyclerview out-of-memory

我在Sql Server中将图像存储为类型'图像'(字节) 现在我会在我的应用程序android中恢复它 我尝试将其作为字节64发送到android但我的内存不足问题 我找到了一些解决方案,比如从php发送图像链接并将其加载到android中,但我不知道如何获取图像的URL(字节)并发送它?

RecyclerView适配器:

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.MyTag>{
LayoutInflater inflater;
DbConnection db ;
CnxExternal myConex = new CnxExternal();
ArrayList<Map<String, String>> listitem;
private CardAdapter.MyTag holder;
Context ctx;
ArrayList<byte[]> img , imgfou ;
ArrayList<String>nomesFo,descriptions;
String codeBar;
ArrayList<Integer>idfourns;



public CardAdapter(Context context, ArrayList<Map<String, String>> list,ArrayList<byte[]>imagbyt,ArrayList<String>names,ArrayList<Integer>idf,String cod,ArrayList<String>Description,ArrayList<byte[]>imagfour) {
    super();
    this.inflater = LayoutInflater.from(context);
    this.listitem = list;
    ctx=context;
    this.img=imagbyt;
    this.nomesFo=names;
    this.idfourns=idf;
    this.codeBar=cod;
    this.imgfou=imagfour;
    this.descriptions=Description;
    db =new DbConnection(ctx);

}

public class MyTag extends RecyclerView.ViewHolder {
    TextView price;
    TextView name;
    ImageView img;
    TextView namfou;
    TextView rating;
    FloatingActionButton btnlike;

    CardView cardView;
    public MyTag(View view) {
        super(view);
        //for animat
cardView=(CardView)view.findViewById(R.id.card_view);

        price = (TextView) view.findViewById(R.id.prixprodtestcard);
        name = (TextView) view.findViewById(R.id.nomprodtestcard);
        namfou=(TextView) view.findViewById(R.id.qtestockcard);
        rating=(TextView)view.findViewById(R.id.rating);
        img = (ImageView) view.findViewById(R.id.imageprodcard);
        btnlike = (FloatingActionButton) view.findViewById(R.id.likeButtoncard);
}
}


@Override
public MyTag onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, null);

    return new MyTag(v);
}


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


@Override
public void onViewRecycled(MyTag holder) {
    super.onViewRecycled(holder);
    Glide.clear(holder.img);
}

public void onBindViewHolder(final MyTag holder, final int position) {

    //anim fadein
    YoYo.with(Techniques.SlideInUp)
            .playOn(holder.cardView);

    final HashMap<String, String> map = (HashMap<String, String>)listitem.get(position);


    if(map.get("like").equals("0")){
        holder.btnlike.setImageResource(R.drawable.heart);
    }else{
        holder.btnlike.setImageResource(R.drawable.ic_liked);

    }
    holder.img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(ctx,InformationsProduit.class);
            i.putExtra("nomProd",map.get("name"));
            i.putExtra("prix",map.get("price"));
            i.putExtra("nomFour",nomesFo.get(position));
            i.putExtra("image",img.get(position));
            i.putExtra("telFour",map.get("telefournisseur"));
            i.putExtra("addrFour",map.get("adressFou"));
            i.putExtra("idFour",idfourns.get(position));
           i.putExtra("description",descriptions.get(position));
            i.putExtra("codeBar",codeBar);
            ctx.startActivity(i);
        }
    });

    holder.price.setText(map.get("price"));
    holder.name.setText(map.get("name"));
    holder.namfou.setText(nomesFo.get(position));
    holder.rating.setText(map.get("rating"));


    Glide.with(ctx)
            .load(imgfou.get(position))
            //.diskCacheStrategy( DiskCacheStrategy.NONE )
            //.skipMemoryCache( true )
            .thumbnail(0.1f)
            .into(holder.img);


    holder.btnlike.setOnClickListener(new View.OnClickListener() {
        CardAdapter.MyTag hld=holder;
        int pos=position;
        @Override
        public void onClick(View arg0) {

            HashMap<String, String> mapclicked = (HashMap<String, String>)listitem.get(pos);
            if(mapclicked.get("like").equals("0"))
            {
                hld.btnlike.setImageResource(R.drawable.ic_liked);
                mapclicked.put("like", "1");


                Snackbar snackbar;
                snackbar = Snackbar.make(hld.btnlike, "Bien ajouter au prefere", Snackbar.LENGTH_SHORT);
                View snackBarView = snackbar.getView();
                snackBarView.setBackgroundColor(Color.GREEN);
                TextView textView = (TextView) snackBarView.findViewById(android.support.design.R.id.snackbar_text);
                textView.setTextColor(Color.WHITE);
                snackbar.show();

                db.insertFav(codeBar,idfourns.get(pos) , map.get("name") , map.get("price") , nomesFo.get(position) , map.get("adressFou") , map.get("telefournisseur") ,img.get(position) );

                hld.btnlike.startAnimation(animAdp());

            }else{
                hld.btnlike.setImageResource(R.drawable.heart);
                mapclicked.put("like", "0");

        Snackbar.make(hld.btnlike, "Removed from prefere" , Snackbar.LENGTH_LONG).setAction("Action", null).show();

                db.deleteR(codeBar,idfourns.get(pos));
                hld.btnlike.startAnimation(animAdp());
            }
        }}
    );



}`

PHP函数:

$res=sqlsrv_next_result($stmt);
while($res){

    while($data=sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
    {
    array_push($information,array(
            ...
            "image"=>base64_encode($data['image']),

            "imgfour"=>base64_encode($data['imagefournisseur']),
                    ));

}
    $res=sqlsrv_next_result($stmt);
}
echo json_encode(array('produits'=>$information));

        }

******************问题*********************** 问题是因为这个大的json文件和带有图像的大型arraylist。不是使用二十个图像的Recyclerview。 @greenapps

3 个答案:

答案 0 :(得分:0)

更好的方法是将您的图像存储在服务器上,可以使用此图像的http公开访问 - Glide

如果您有路径,那么您可以使用适用于Android的图片加载库(PicassoFrescoSQL INSERT INTO from multiple tables)在您的应用中高效加载它,而不会出现内存不足异常

答案 1 :(得分:0)

为什么要将字节数组编码为base64字符串?只是回显字节。你不必在服务器上创建一个字节文件 提出了两个答案。

输出字节的php脚本与服务器上图像文件的直接链接一样好。

答案 2 :(得分:0)

你可以使用这样的链接

https://pbs.twimg.com/profile_images/450103729383956480/Tiys3m4x.jpeg

表单服务器并在android中显示然后使用&#34;毕加索&#34;图像加载库