如何在android中的gridview中添加所选图像

时间:2017-10-10 08:02:19

标签: android arrays

我想从图库中选择一些图像并在gridview中显示。我使用过Filepicker库。它的工作但我面临的问题是图像在选择后没有显示在gridview中。我告诉你我的代码。请帮我。 这是我的片段类

   fabPhotosAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FilePickerBuilder.getInstance().setSelectedFiles(arrayList)
                    .setActivityTheme(R.style.Theme_AppCompat_Light).pickPhoto(getActivity());
        }
    });

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case FilePickerConst.REQUEST_CODE:
            if (resultCode == RESULT_OK && data != null) {
                arrayList = data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_PHOTOS);
                PhotoFragmentModel model;
                ArrayList<PhotoFragmentModel> list = new ArrayList<>();
                try {
                    for (String path : arrayList) {
                        model = new PhotoFragmentModel();
                        model.setName(path.substring(path.lastIndexOf("/") + 1));
                        model.setUri(Uri.fromFile(new File(path)));
                        list.add(model);
                    }
                    gridPhotos.setAdapter(new PhotoFragmentAdapter(getActivity(), list));
                    Toast.makeText(getActivity(), "Total : " + String.valueOf(list.size()), Toast.LENGTH_SHORT).show();
                } catch (Exception e) {

                }
            }
    }
}

PhotoFragmentAdapter.java

public class PhotoFragmentAdapter extends BaseAdapter {

private Context context;
private ArrayList<PhotoFragmentModel> arrayList;
private LayoutInflater inflater;
private ImageView ivPhoto;
private TextView tvName;

public PhotoFragmentAdapter(Context context, ArrayList<PhotoFragmentModel> arrayList) {
    this.context = context;
    this.arrayList = arrayList;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return arrayList.size();
}

@Override
public Object getItem(int i) {
    return arrayList.get(i);
}

@Override
public long getItemId(int i) {
    return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    view = inflater.inflate(R.layout.item_photo_fragment, null);
    final PhotoFragmentModel model = (PhotoFragmentModel) this.getItem(i);

    ivPhoto = (ImageView) view.findViewById(R.id.iv_item_photo);
    tvName = (TextView) view.findViewById(R.id.tv_item_name);

    Picasso.with(context).load(model.getUri()).fit().into(ivPhoto);
    tvName.setText(model.getName());
    return view;
}

}

选择工作正常,但选择后图像未显示在gridview中。这是主要问题。如果有人可以帮助我或指导我其他图书馆,我会感谢他。

1 个答案:

答案 0 :(得分:1)

model.setUri(Uri.fromFile(new File(path)));

这个案例无法解决:文件夹的特殊字符。

试试这个

model.setUri(Uri.fromFile(new File(getPath(uri)));

public String getPath(Uri uri) 
    {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
        if (cursor == null) return null;
        int column_index =             cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String s=cursor.getString(column_index);
        cursor.close();
        return s;
    }

哦,现在我知道了不工作的主要原因是

final PhotoFragmentModel model = (PhotoFragmentModel) this.getItem(i);

你可以直接调用索引

的arraylist项
Picasso.with(context).load(arrayList.get(i).getUri()).fit().into(ivPhoto);