Matisse: How to hide types that are not allowed from Matisse?

Created on 6 Jun 2017  ·  9Comments  ·  Source: zhihu/Matisse

Hi guys, me again, I've made a fork of this library because I had to make some minor changes in order to use it into one of my projects, some of them I've already pull requested here, but the point is that I need to hide files that are not allowed by my application, for example, if I have Matisse.from(this).choose(MimeType.ofVideo(), false) I want to hide all the media that aren't video files, that's an UX request from my development team, I've tried to change the AlbumMediaAdapter by filtering and I have some success, but I still have white frames in the RecyclerView, I don't get why, even trying to use notifyItemChanged or notifyDatasetChanged these gaps don't move away. Do you know why or there's a better way to achieve this?

These are the changes that I've made so far, I know that's not the best option doing this on adapter, but I need to achieve a fast prototype of this feature.

@Override
    protected void onBindViewHolder(final RecyclerView.ViewHolder holder, Cursor cursor) {
        final Item item = Item.valueOf(cursor);

        if (holder instanceof CaptureViewHolder) {
            CaptureViewHolder captureViewHolder = (CaptureViewHolder) holder;
            Drawable[] drawables = captureViewHolder.mHint.getCompoundDrawables();
            TypedArray ta = holder.itemView.getContext().getTheme().obtainStyledAttributes(
                    new int[]{R.attr.capture_textColor});
            int color = ta.getColor(0, 0);
            ta.recycle();
            for (Drawable drawable : drawables) {
                if (drawable != null) {
                    drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
                }
            }
        } else if (holder instanceof MediaViewHolder) {
            MediaViewHolder mediaViewHolder = (MediaViewHolder) holder;

            if (mSelectionSpec.mimeTypeSet.toString().contains(item.mimeType)) {
                mediaViewHolder.mMediaGrid.preBindMedia(new MediaGrid.PreBindInfo(
                        getImageResize(mediaViewHolder.mMediaGrid.getContext()),
                        mPlaceholder,
                        mSelectionSpec.countable,
                        holder
                ));

                mediaViewHolder.mMediaGrid.bindMedia(item);
                mediaViewHolder.mMediaGrid.setOnMediaGridClickListener(this);
                setCheckStatus(item, mediaViewHolder.mMediaGrid);
            } else {
                mediaViewHolder.mMediaGrid.setVisibility(View.GONE);
            }
        }
    } 

Thanks in advance. :)

Most helpful comment

@bruferrari you can use https://github.com/zhihu/Matisse/releases/tag/v0.5.0-alpha4 just like this:

Matisse.from(MainActivity.this)
    ...
    .showSingleMediaType(true)
    .forResult(REQUEST_CODE);

All 9 comments

Ok, I realized that these gaps are from the cursor size, but there's a way to make this validation before the cursor come into onBindViewHolder?

@bruferrari
This is a feature requested by a few people not only you. You've made a great proposal.

As you figured out, this is because the data source which is a cursor, always has all the media data including images and videos. I think a better solution is to query the very needed data source in the first place. Specifically,

  • Provide a interface for whether to hide types not wanted.
  • In AlbumLoader and AlbumMediaLoader, alter selection parameter against the query.

But before that, I have a question, do you want to hide every specific mime types (like only image/gif and image/webp) or you just want to hide a general media type (like hide all images or all videos)? And for all the other potential users, which way do you think is a better one?

And also, do you want to work on this feature since you have digged into it.

@gejiaheng I need to hide just general mime types, i.e. if only allowed user to see images, so all the videos containing on gallery will be hide, changing the adapter is fast but dirty solution because of the cursor itself.

I've been working the last few weeks on a project that are a in-company social network and it uses Matisse as primary library for pick images, videos and so on, so I'm headed to solve this gap, but I would like to have more opinions about the best way to do it, the last thing that I've thought was trying to change the cursor returned by AlbumMediaLoader by filtering the mime types of it.

The iOS for example has this feature as native I believe, exactly as I've requested, displaying or hiding just the general mime types, so for the first step I think that's the best option, more improvements could be in future like filtering by specific mime type.

I'll be glad to help on this feature as soon as I have some time to spend on it. :)

I'd opened PR #96 with the workaround for general mimetypes, thanks for your tips! :)

@bruferrari you can use https://github.com/zhihu/Matisse/releases/tag/v0.5.0-alpha4 just like this:

Matisse.from(MainActivity.this)
    ...
    .showSingleMediaType(true)
    .forResult(REQUEST_CODE);

many thanks @gejiaheng

@bruferrari You're welcome.

How can I hide every specific mime types (like only image/gif and image/webp) or I want to show only jpeg images?

I do edit the AlbumLoader/AlbumMediaLoader of Matisse(import by code), and the selection in that two file.

  • " AND " + MediaStore.MediaColumns.MIME_TYPE + "!='image/gif'"
Was this page helpful?
0 / 5 - 0 ratings

Related issues

zzzhoukang picture zzzhoukang  ·  5Comments

hugoFather picture hugoFather  ·  5Comments

GTooo picture GTooo  ·  6Comments

naivor picture naivor  ·  7Comments

aliondos picture aliondos  ·  3Comments