我将图像从相机和图库(多选)保存到内部存储中的文件夹。我想在水平Recyclerview中显示这些图像。在行布局中,有一个关闭按钮以及imageview。我想在单击关闭按钮时从内部存储中删除图像(文件)。我有两个问题: 问题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();
}
}
}
&#13;
//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();
&#13;
答案 0 :(得分:0)
我不明白你的第一部分解释得更多。
在第二部分中,将图像路径设置为imageView标记非常简单。
imageView.setTag(imageAbsolutePath);
当你点击图片时从图片视图中获取标签并将其转换为像这样的文件
File path = new File(imageView.getTag().toString());
path.delete();