在ViewHolder中设置ImageView从另一个活动获取Uri列表

时间:2017-06-15 12:55:59

标签: android android-recyclerview android-imageview

好的问题是我选择了多个图像,并且必须在另一个活动回收站视图(网格视图)中显示所有选定的图像。我在第一个活动中获得了Uri路径(也尝试过字符串)。

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    try {
        // When an Image is picked
        if (requestCode == SELECT_PICTURE_MULTIPLE && resultCode == RESULT_OK
                && null != data) {
            // Get the Image from data

            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            imagesEncodedList = new ArrayList<Uri>();
            if(data.getData()!=null){

                Uri mImageUri=data.getData();

                // Get the cursor
                Cursor cursor = getContentResolver().query(mImageUri,
                        filePathColumn, null, null, null);
                // Move to first row
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imageEncoded  = Uri.parse(cursor.getString(columnIndex));
                imagesEncodedList.add(imageEncoded);
                cursor.close();

            }else {
                if (data.getClipData() != null) {
                    ClipData mClipData = data.getClipData();
                    ArrayList<Uri> mArrayUri = new ArrayList<Uri>();
                    for (int i = 0; i < mClipData.getItemCount(); i++) {

                        ClipData.Item item = mClipData.getItemAt(i);
                        Uri uri = item.getUri();
                        mArrayUri.add(uri);
                        // Get the cursor
                        Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
                        // Move to first row
                        cursor.moveToFirst();

                        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                        imageEncoded  = Uri.parse(cursor.getString(columnIndex));
                        imagesEncodedList.add(imageEncoded);
                        cursor.close();

                    }
                    Log.v("LOG_TAG", "Selected Images" + mArrayUri.size());
                }
            }
        } else {
            Toast.makeText(this, "You haven't picked Image",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                .show();
    }

    super.onActivityResult(requestCode, resultCode, data);
}

在第二项活动中我得到了uris

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    images=new ArrayList<Uri>();
    images=(ArrayList) getIntent().getSerializableExtra("IMAGES_LIST");

    RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerViewGrid);
    recyclerView.setLayoutManager(new GridLayoutManager(this,2));
    recyclerViewAdapter = new RecyclerViewAdapter(images,this);

    recyclerView.setAdapter(recyclerViewAdapter);
}

RecyclerViewAdapter是:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.PhotoHolder> {
private Context context;
private ArrayList<Uri> photos;



public RecyclerViewAdapter(ArrayList<Uri> photos,Context context){
    this.photos=photos;
    this.context=context;
}
@Override
public PhotoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.photo_grid,parent,false);
    PhotoHolder photoHolder = new PhotoHolder(view);
    return photoHolder;
}

@Override
public void onBindViewHolder(PhotoHolder holder, int position) {
    Uri photo = photos.get(position);
    String path = photo.getPath();
    File sd = Environment.getExternalStorageDirectory();
    File image = new File(path);
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
    //bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
    holder.imageViewGrid.setImageBitmap(bitmap);
    holder.textViewGrid.setText(photo+"");
}

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

public class PhotoHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    public ImageView imageViewGrid;
    public TextView textViewGrid;
    public PhotoHolder(View itemView) {
        super(itemView);
        imageViewGrid = (ImageView)itemView.findViewById(R.id.imageViewGrid);
        textViewGrid = (TextView)itemView.findViewById(R.id.textViewGrid);
    }

    @Override
    public void onClick(View view) {

    }
}
}

现在即使我有路径,图像视图也没有获取图像。我尝试过Picasso,Glide和几乎所有可能的选择。仍然没有发现错误。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

你可以这样做:

holder.imageViewGrid.setImageURI(null); 
holder.imageViewGrid.setImageURI(photo);

首先设置null以重置它。

让我知道结果