2017-07-25 92 views
0

我正在尝试使用Recycler视图和Sub Sampling图库实现图库应用程序。 由于我的图片数量大约为850.当我尝试将图片加载到图库中时,图库滞后。当我尝试从SD卡(Android Recyclerview)中加载图片时,我的图库存在很多问题

这里是我的Recyclerview适配器: -

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolders> { 

private ArrayList<String> yeniliste; 
private Context context; 

public RecyclerViewAdapter(Context context, ArrayList<String> itemList) { 
    this.yeniliste = itemList; 
    this.context = context; 
} 

@Override 
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) { 
    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.gallery_item, null); 
    RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView); 
    return rcv; 
} 

@Override 
public void onBindViewHolder(final RecyclerViewHolders holder, final int position) { 
    try { 
     Bitmap bitmap = BitmapFactory.decodeFile(yeniliste.get(position)); 
     holder.countryPhoto.setImage(ImageSource.bitmap(bitmap).dimensions(50,50)); 
    }catch (Exception e){ 
     e.printStackTrace(); 
    } 

    holder.countryPhoto.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(v.getContext(),GalleryFullImage.class); 
      intent.putExtra("realid",String.valueOf(holder.getAdapterPosition())); 
      v.getContext().startActivity(intent); 
     } 
    }); 
} 

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


public static class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener{ 

    public SubsamplingScaleImageView countryPhoto; 

    public RecyclerViewHolders(View itemView) { 
     super(itemView); 
     countryPhoto = (SubsamplingScaleImageView)itemView.findViewById(R.id.country_photo); 
    } 

    @Override 
    public void onClick(View view) { 
     Toast.makeText(view.getContext(), "Clicked Country Position = " + getAdapterPosition(), Toast.LENGTH_SHORT).show(); 

    } 

} 

public void removeItem(int position) 
{ 
    yeniliste.remove(position); 
    notifyDataSetChanged(); 
}} 
+1

这可能需要太长时间:“位图位图= BitmapFactory.decodeFile(yeniliste.get(position));”尝试使用像毕加索或滑翔的图书馆。主要原因在于正在主线程中完成。如果你不想使用库,那么你需要在不同的线程中执行该操作 –

+0

我试过用picasso加载,但图像不加载 –

+0

您是否试过滑行:https://stackoverflow.com/questions/34443171 /负载图像从-SD-卡使用滑行。你可以检查此情况下的毕加索:https://stackoverflow.com/questions/24097576/how-to-load-image-from-sd-card-using-picasso-library –

回答

0
Bitmap bitmap = BitmapFactory.decodeFile(yeniliste.get(position)); 

有在此调用一些问题:

首先,加载到内存时,你是不是downscaling the image。如果您开始获得应用程序抛出的大量内存不足异常,我不会感到惊讶。

其次,您正在UI线程中加载图像,这是一项昂贵的操作,会导致您遇到的延迟:您的应用程序将无法在从磁盘加载图像时呈现新帧。您需要使用后台线程来完成这项工作。做这件事最方便的方法是使用异步任务,一个例子描述为here

但最好的选择是使用库来处理这个给你。我真的推荐Glide。它已经可以处理内存缩减,后台加载,快速重新加载的缓存,再加上它有一个非常直观的API。

此代码:

Bitmap bitmap = BitmapFactory.decodeFile(yeniliste.get(position)); 
holder.countryPhoto.setImage(ImageSource.bitmap(bitmap).dimensions(50,50); 

将变为:

GlideApp.with(context).load(eniliste.get(position)).override(50,50).centerCrop().into(new SimpleTarget<Bitmap>(50, 50) { 
     @Override 
     public void onResourceReady(Bitmap bitmap, GlideAnimation anim) { 
     holder.countryPhoto.setImage(ImageSource.bitmap(bitmap)); 
     ); 

当你使用的不是简单的ImageView作为位图的持有者,您将需要使用custom target implementation

我真的推荐阅读文档或作为关于滑行的问题,如果您在执行此操作时遇到问题。

+0

String completePath = yeniliste.get(position); File file = new File(completePath); Uri imageUri = Uri.fromFile(file); glide.with(context).load(imageUri).into(holder.countryPhoto); ......图像不使用滑动加载。我以正确的方式行事吗? –

+0

您用来加载图像的上下文是否有效?你出于某种原因摧毁它吗? –

+0

是的..相同的情况下 –

相关问题