我想从图库中选择一些图像并在gridview中显示。我使用了Filepicker库。它的工作,但我面临的mai问题是选择后在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中显示。这是主要问题。如果有人可以帮助我或指导我其他图书馆,我将感谢他。

最佳答案

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

关于android - 如何在Android的GridView中添加选定的图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46661521/

10-10 23:35