在ViewHolder中设置ImageView从另一个活动获取Uri列表 [英] Set an ImageView in a ViewHolder getting Uri List from another activity

查看:166
本文介绍了在ViewHolder中设置ImageView从另一个活动获取Uri列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Okay the problem is that I am selecting multiple images and have to display all the selected images in another activity recycler view(Grid View). I got the Uri paths(Tried string as well) in the first activity.

    @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);
}

在第二项活动中,我得到了尿毒症

And in the Second Activity I am getting the 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是:

The RecyclerViewAdapter is:

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) {

    }
}
}

现在,即使我有路径,图像视图也无法获取图像.我已经尝试过毕加索,滑翔机和几乎所有我能想到的可能选择.仍未发现错误. 任何帮助将不胜感激.

Now even though I have the path the image view is not getting the image. I have tried Picasso, Glide, and almost all the possible options I could conceive. Still have not found the error. Any help will be appreciated.

推荐答案

您可以通过以下代码来做到:

You can do it By this code:

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

只需先将其设置为null即可重置

just first set null to reset it.

让我知道结果

这篇关于在ViewHolder中设置ImageView从另一个活动获取Uri列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆