我正在将相机和图库(多选)中的图像保存到内部存储的文件夹中,并希望在水平RecyclerView中显示这些图像。 在行布局中,除了ImageView之外,还有一个关闭按钮。当单击该关闭按钮时,我希望从内部存储中删除图像(文件)。 我有2个问题:
问题1: 当我从图库中选择图像(单个或多个),图像会正确保存,但在Recyclerview中,来自图库的图像将在所有位置显示,即如果文件夹中有2个图像并且我从图库中选择1个图像,则在Recyclerview中所有3个位置都只有来自图库的图像。
问题2: 如何在单击关闭按钮时从文件夹中删除图像?
//Recyclerview Adapter
public class RecycleImageAdapter extends RecyclerView.Adapter<RecycleImageAdapter.ViewHolder> {
private List<String> itemList;
private Context mContext;
public RecycleImageAdapter(Context context, List<String> itemList) {
this.itemList = itemList;
this.mContext = context;
}
@Override
public RecycleImageAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.imagelayout, parent, false);
return new RecycleImageAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(final RecycleImageAdapter.ViewHolder holder, int position) {
for (int i= 0; i < itemList.size(); i++) {
Uri uri = Uri.parse(itemList.get(i));
holder.setImageView(uri);
}
}
@Override
public int getItemCount() {
return this.itemList.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
ImageView image, delbtn;
ViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.img_thumbnail);
delbtn = (ImageView)itemView.findViewById(R.id.del);
delbtn.setOnClickListener(this);
}
public void setImageView(Uri uri){
Picasso.with(mContext).load(uri).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
image.setImageBitmap(bitmap);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
Log.e("sammy_error"," "+errorDrawable);
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
}
@Override
public void onClick(View view) {
int position = getAdapterPosition();
Toast.makeText(mContext, itemList.get(position), Toast.LENGTH_SHORT).show();
itemList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, itemList.size());
//Here I'm getting ArrayIndexOutOfBoundsException
File file = new File(itemList.get(position));
file.delete();
}
}
}
//in Fragment, code to load images
File directory = new File(file_path);
File newfile[] = directory.listFiles();
//finalImages is ArrayList<String>
if(finalImages!=null )finalImages.clear();
for (int i= 0; i < newfile.length; i++) {
String path = "file://"+newfile[i].getAbsolutePath();
finalImages.add(path);
}
recyladapter.notifyDataSetChanged();