public class PictureGridAdapter extends RecyclerView.Adapter<ItemPictureGridHolder> {

    private final int ITEM_TYPE_VIEW_CAMERA = 1;
    private final int ITEM_TYPE_VIEW_IMAGE = 2;

    private OnItemClickListener onItemClickListener;

    private Activity activity;
    private List<PictureItem> pictureItemList;

    private PicturePicker picturePicker;
    private boolean isShowCamera;
    private boolean isMultiMode;

    public PictureGridAdapter(Activity activity, List<PictureItem> pictureItemList) {
        this.activity = activity;
        this.pictureItemList = pictureItemList;
        picturePicker = PicturePicker.getInstance();
        isShowCamera = picturePicker.getPickPictureOptions().isShowCamera();
        isMultiMode = picturePicker.getPickPictureOptions().isMultiMode();
    }


    @Override
    public int getItemCount() {
        if (pictureItemList == null) {
            return isShowCamera ? 1 : 0;
        } else {
            return isShowCamera ? pictureItemList.size() + 1 : pictureItemList.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 && isShowCamera ? ITEM_TYPE_VIEW_CAMERA : ITEM_TYPE_VIEW_IMAGE;
    }

    @Override
    public ItemPictureGridHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(activity).inflate(R.layout.item_picture_grid, parent, false);
        int itemViewWidth = (parent.getRight() - parent.getLeft()) / PictureGridActivity.RECYCLER_VIEW_COLUMN;
        //设置宽高 使其为正方形
        itemView.setLayoutParams(new RecyclerView.LayoutParams(itemViewWidth, itemViewWidth));
        return new ItemPictureGridHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final ItemPictureGridHolder holder,  int position) {

        switch (getItemViewType(position)) {
            case ITEM_TYPE_VIEW_CAMERA:
                holder.getIvPickPicture().setImageResource(R.drawable.release_takepic);
                holder.getIvImgSelected().setVisibility(View.GONE);
                holder.getViewPickerPictureCover().setVisibility(View.GONE);
                break;
            case ITEM_TYPE_VIEW_IMAGE:

                final PictureItem pictureItem = isShowCamera ? pictureItemList.get(position - 1) : pictureItemList.get(position);
                ImageLoader.load(activity, pictureItem.pictureAbsPath, holder.getIvPickPicture());
                if (isMultiMode) {
                    holder.getIvImgSelected().setVisibility(View.VISIBLE);
                } else {
                    holder.getIvImgSelected().setVisibility(View.GONE);
                }

                holder.getViewPickerPictureCover().setVisibility(View.VISIBLE);
                refreshItemView(picturePicker.isSelected(pictureItem), holder);
                holder.getIvImgSelected().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        addOrRemovePicture(pictureItem, holder);
                    }
                });
                break;
            default:
                break;
        }


        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(holder, holder.getAdapterPosition());
                }
            });
        }

    }

    public void addOrRemovePicture(PictureItem pictureItem, ItemPictureGridHolder holder) {
        if (picturePicker.isSelected(pictureItem)) {
            picturePicker.addOrRemovePicture(pictureItem, false);
            refreshItemView(false, holder);
        } else {
            int maxCount = picturePicker.getPickPictureOptions().getPickMaxCount();
            if (picturePicker.getCurrentSelectedCount() >= maxCount) {
                Utils.showToast(activity, activity.getResources().getString(R.string.select_limit_tips, maxCount + ""));
            } else {
                picturePicker.addOrRemovePicture(pictureItem, true);
                refreshItemView(true, holder);
            }
        }
    }


    private void refreshItemView(boolean isSelected, ItemPictureGridHolder holder) {
        holder.getViewPickerPictureCover().setSelected(isSelected);
        holder.getIvImgSelected().setSelected(isSelected);
    }


    public interface OnItemClickListener {
        void onItemClick(ItemPictureGridHolder holder, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


} public class PopupFolderListAdapter extends BaseAdapter {


    private String currentSelectFolderPath;

    private Context context;
    private List<PictureFolder> pictureFolderList;

    public PopupFolderListAdapter(Context context, List<PictureFolder> pictureFolderList) {
        this.context = context;
        this.pictureFolderList = pictureFolderList;
    }

    @Override
    public int getCount() {
        return pictureFolderList == null ? 0 : pictureFolderList.size();
    }

    @Override
    public Object getItem(int position) {
        return pictureFolderList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ItemFolderListHolder holder;
        if (convertView == null) {
            convertView = inflate(context, R.layout.item_folder_list, null);
            holder = new ItemFolderListHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ItemFolderListHolder) convertView.getTag();
        }

        PictureFolder pictureFolder = pictureFolderList.get(position);
        ImageLoader.load(context, pictureFolder.folderCover.pictureAbsPath, holder.getIvFolderCover());

        holder.getTvFolderName().setText(pictureFolder.folderName);
        holder.getTvPictureCount().setText(context.getResources().getString(R.string.folder_picture_count,pictureFolder.pictureItemList.size()));

        if (TextUtils.equals(currentSelectFolderPath, pictureFolder.folderAbsPath)) {
            holder.getIvFolderCheck().setVisibility(View.VISIBLE);
        } else {
            holder.getIvFolderCheck().setVisibility(View.GONE);
        }

        return convertView;
    }

    /**
     * 获取单个itemView的高度
     *
     * @return
     */
    public int getItemViewHeight() {

        View itemView = View.inflate(context, R.layout.item_folder_list, null);
        itemView.measure(0, 0);
        return itemView.getMeasuredHeight();
    }

    /**
     * 获取当前选择的文件夹路劲
     *
     * @return
     */
    public String getCurrentSelectFolderPath() {

        return currentSelectFolderPath;
    }


    public void notifyDataSetChanged(String currentSelectFolderPath) {
        this.currentSelectFolderPath = currentSelectFolderPath;
        notifyDataSetChanged();
    }

} public class ItemPictureGridHolder extends RecyclerView.ViewHolder {
    private ImageView ivPickPicture;
    private View viewPickerPictureCover;
    private ImageView ivImgSelected;

    public ItemPictureGridHolder(LayoutInflater inflater, ViewGroup parent) {
        this(inflater.inflate(R.layout.item_picture_grid, parent, false));
    }

    public ItemPictureGridHolder(View view) {
        super(view);
        ivPickPicture = (ImageView) view.findViewById(R.id.iv_pick_picture);
        viewPickerPictureCover = view.findViewById(R.id.view_picker_picture_cover);
        ivImgSelected = (ImageView) view.findViewById(R.id.iv_img_selected);
    }

    public ImageView getIvPickPicture() {
        return ivPickPicture;
    }

    public View getViewPickerPictureCover() {
        return viewPickerPictureCover;
    }

    public ImageView getIvImgSelected() {
        return ivImgSelected;
    }
} public enum PreviewAction implements Serializable {

    /**
     * 只预览
     */
    ONLY_PREVIEW,
    /**
     * 预览并选择图库相片
     */
    PREVIEW_PICK,
    /**
     * 预览并可删除图片
     */
    PREVIEW_DELETE


} public class PictureFolder implements Serializable {

    /**
     * 图片文件夹名称
     */
    public String folderName;
    /**
     * 图片文件夹绝对路径
     */
    public String folderAbsPath;
    /**
     *  图片文件夹需要要显示的封面
     */
    public PictureItem folderCover;
    /**
     * 图片文件夹下所有的图片
     */
    public List<PictureItem> pictureItemList;

    @Override
    public boolean equals(Object o) {
        if (o instanceof PictureFolder){
            PictureFolder folder= (PictureFolder) o;
            return TextUtils.equals(folder.folderAbsPath,folderAbsPath);
        }
        return super.equals(o);
    }
} public class PictureItem implements Serializable {

    /**
     * 图片名称
     */
    public String pictureName;
    /**
     * 图片绝对路径
     */
    public String pictureAbsPath;
    /**
     * 图片类型
     */
    public String pictureMimeType;
    /**
     * 图片大小
     */
    public long pictureSize;
    /**
     * 图片加入的时间
     */
    public long pictureAddTime;
    /**
     * 图片的宽
     */
    public int pictureWidth;
    /**
     * 图片的高
     */
    public int pictureHeight;


    @Override
    public boolean equals(Object o) {
        if (o instanceof PictureItem) {
            PictureItem pictureItem = ((PictureItem) o);
            return TextUtils.equals(pictureItem.pictureAbsPath, pictureAbsPath)
                    &&
                    pictureItem.pictureAddTime == pictureAddTime;
        }
        return super.equals(o);
    }
} public class PickOptions implements Serializable {

    private boolean justTakePhoto;
    private int pickMaxCount;
    private boolean multiMode;
    private boolean showCamera;
    private boolean canPreviewImg;
    private boolean selectGif;

    private PickOptions(Builder builder) {
        this.justTakePhoto = builder.justTakePhoto;
        this.pickMaxCount = builder.pickMaxCount;
        this.multiMode = builder.multiMode;
        this.showCamera = builder.showCamera;
        this.canPreviewImg = builder.canPreviewImg;
        this.selectGif = builder.selectGif;
    }

    private PickOptions() {
    }

    public boolean isJustTakePhoto() {
        return justTakePhoto;
    }

    public int getPickMaxCount() {
        return pickMaxCount;
    }

    public boolean isMultiMode() {
        return multiMode;
    }

    public boolean isShowCamera() {
        return showCamera;
    }

    public boolean isCanPreviewImg() {
        return canPreviewImg;
    }

    public boolean isSelectGif() {
        return selectGif;
    }

    public static PickOptions getDefaultOptions() {
        return new PickOptions(new Builder());
    }

    public static class Builder implements Serializable {
        private boolean justTakePhoto=false;
        private int pickMaxCount = 9;
        private boolean multiMode = true;
        private boolean showCamera = true;
        private boolean canPreviewImg = true;
        private boolean selectGif = true;

        public Builder() {
        }

        public PickOptions build() {
            return new PickOptions(this);
        }

        public Builder setJustTakePhoto(boolean justTakePhoto) {
            this.justTakePhoto = justTakePhoto;
            return this;
        }

        public Builder setPickMaxCount(int pickMaxCount) {
            this.pickMaxCount = pickMaxCount;
            return this;
        }

        public Builder setMultiMode(boolean multiMode) {
            this.multiMode = multiMode;
            return this;
        }

        public Builder setShowCamera(boolean showCamera) {
            this.showCamera = showCamera;
            return this;
        }

        public Builder setCanPreviewImg(boolean canPreviewImg) {
            this.canPreviewImg = canPreviewImg;
            return this;
        }

        public Builder setSelectGif(boolean selectGif) {
            this.selectGif = selectGif;
            return this;
        }
    }

} public class PicturePicker {

    private ILanguageProvider languageProvider;


    private List<OnPictureSelectedListener> pictureSelectedListeners = new ArrayList<>();
    private List<PictureItem> selectedPictureList = new ArrayList<>();

    private static PicturePicker picturePicker;
    private PickOptions pickPictureOptions;

    private PicturePicker() {
    }

    public static PicturePicker getInstance() {
        if (picturePicker == null) {
            synchronized (PicturePicker.class) {
                if (picturePicker == null) {
                    picturePicker = new PicturePicker();
                }
            }
        }

        return picturePicker;
    }


    /**
     * 默认配置下选择图片
     *
     * @param activity
     * @param requestCode
     */
    public void startPickPicture(Activity activity, int requestCode) {
        startPickPicture(activity, requestCode, PickOptions.getDefaultOptions());
    }

    /**
     * @param activity
     * @param requestCode
     * @param options     选择图片时的配置
     */
    public void startPickPicture(Activity activity, int requestCode, PickOptions options) {

        this.pickPictureOptions = options;
        Intent intent = new Intent(activity, CheckPermissionActivity.class);
        activity.startActivityForResult(intent, requestCode);

    }

    public void startPickPicture(android.app.Fragment fragment, int requestCode) {
        startPickPicture(fragment, requestCode, PickOptions.getDefaultOptions());
    }

    public void startPickPicture(android.app.Fragment fragment, int requestCode, PickOptions options) {
        this.pickPictureOptions = options;
        Intent intent = new Intent(fragment.getActivity(), CheckPermissionActivity.class);
        fragment.startActivityForResult(intent, requestCode);
    }

    public void startPickPicture(android.support.v4.app.Fragment fragment, int requestCode) {
        startPickPicture(fragment, requestCode, PickOptions.getDefaultOptions());
    }

    public void startPickPicture(android.support.v4.app.Fragment fragment, int requestCode, PickOptions options) {
        this.pickPictureOptions = options;
        Intent intent = new Intent(fragment.getActivity(), CheckPermissionActivity.class);
        fragment.startActivityForResult(intent, requestCode);
    }



    /**
     * 清理数据
     */
    public void cleanData() {
        pictureSelectedListeners.clear();
        selectedPictureList.clear();
        pickPictureOptions=null;
    }


    /**
     * 获取当前的选择配置
     * @return
     */
    public PickOptions getPickPictureOptions() {
        return pickPictureOptions;
    }

    public void setPickPictureOptions(PickOptions pickPictureOptions) {
        this.pickPictureOptions = pickPictureOptions;
    }

    /**
     * 获取当前已经选择的图片
     * @return
     */
    public  List<PictureItem> getSelectedPictureList(){
        return selectedPictureList;
    }


    /**
     * 判断某个图片是否被选中
     *
     * @param pictureItem
     * @return
     */
    public boolean isSelected(PictureItem pictureItem) {
        return selectedPictureList.contains(pictureItem);
    }

    /**
     * 获取当前已经选择的图片的数量
     * @return
     */
    public int getCurrentSelectedCount() {
        return selectedPictureList.size();
    }


    public void addOrRemovePicture(PictureItem pictureItem, boolean isSelected) {

        if (isSelected) {
            selectedPictureList.add(pictureItem);
        } else {
            selectedPictureList.remove(pictureItem);
        }

        for (OnPictureSelectedListener listener : pictureSelectedListeners) {
            listener.onPictureSelected(selectedPictureList, pictureItem, isSelected);
        }

    }

    /**
     * 图片选择的监听
     */
    public interface OnPictureSelectedListener {
        void onPictureSelected(List<PictureItem> selectedPictureList, PictureItem pictureItem, boolean isSelected);
    }

    /**
     * 注册图片选择的监听器
     *
     * @param listener
     */
    public void registerPictureSelectedListener(OnPictureSelectedListener listener) {
        if (listener != null) {
            pictureSelectedListeners.add(listener);
        }
    }

    /**
     * 反注册图片选择的监听器
     *
     * @param listener
     */
    public void unregisterPictureSelectedListener(OnPictureSelectedListener listener) {
        if (listener != null) {
            pictureSelectedListeners.remove(listener);
        }
    }

    public ILanguageProvider getLanguageProvider() {
        return languageProvider;
    }

    public void setLanguageProvider(ILanguageProvider languageProvider) {
        this.languageProvider = languageProvider;
    }
} public class PictureScanner implements LoaderManager.LoaderCallbacks<Cursor> {

    private final int SCANNER_ID = 0;

    /**
     * 查询图片需要的数据列
     */
    private final String[] IMAGE_PROJECTION = {
            ////图片的显示名称  aaa.jpg
            MediaStore.Images.Media.DISPLAY_NAME,
            //图片的真实路径  /storage/emulated/0/pp/downloader/wallpaper/aaa.jpg
            MediaStore.Images.Media.DATA,
            //图片的大小，long型  132492
            MediaStore.Images.Media.SIZE,
            //图片的宽度，int型  1920
            MediaStore.Images.Media.WIDTH,
            //图片的高度，int型  1080
            MediaStore.Images.Media.HEIGHT,
            //图片被添加的时间，long型  1450518608
            MediaStore.Images.Media.DATE_ADDED,
            //图片的类型     image/jpeg
            MediaStore.Images.Media.MIME_TYPE};


    /**
     * 图片扫描成的回调接口
     */
    public interface OnScanFinishListener {
        /**
         * 扫描完成
         *
         * @param pictureFolders
         */
        void onScanFinish(List<PictureFolder> pictureFolders);
    }

    /**
     * 所有的目录集合
     */
    private List<PictureFolder> pictureFolderList = new ArrayList<>();
    private LoaderManager loaderManager;

    private Context context;
    private String scanFolderAbsPath;
    private OnScanFinishListener scanFinishListener;


    public PictureScanner() {
    }

    /**
     * 扫描指定路径下图片
     *
     * @param scanFolderAbsPath
     */
    public PictureScanner(String scanFolderAbsPath) {
        this.scanFolderAbsPath = scanFolderAbsPath;
    }

    /**
     * 开始扫描
     *
     * @param scanFinishListener 扫描完成监听
     */
    public void startScanPicture(Activity activity, OnScanFinishListener scanFinishListener) {

        this.context = activity.getApplication();
        this.scanFinishListener = scanFinishListener;

        //先停止扫描
        stopScanPicture();

        loaderManager = activity.getLoaderManager();
        loaderManager.initLoader(SCANNER_ID, null, this);
    }

    /**
     * 停止扫描
     */
    public void stopScanPicture() {
        if (loaderManager != null) {
            loaderManager.destroyLoader(SCANNER_ID);
        }
    }


    private Loader<Cursor> createLoader() {

        CursorLoader loader = new CursorLoader(context);

        loader.setUri(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        //要查询的数据
        loader.setProjection(IMAGE_PROJECTION);

        String selection = MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=?";

        if (TextUtils.isEmpty(scanFolderAbsPath)) {
            loader.setSelection(selection);
            String[] selectionArgs;
            if (PicturePicker.getInstance().getPickPictureOptions().isSelectGif()) {
                selectionArgs = new String[]{"image/jpeg", "image/png", "image/jpg", "image/gif"};
            } else {
                selectionArgs = new String[]{"image/jpeg", "image/png", "image/jpg"};
            }
            loader.setSelectionArgs(selectionArgs);
        } else {
            selection = selection + " " + MediaStore.Images.Media.DATA + " like '%" + scanFolderAbsPath + "%'";
            loader.setSelection(selection);
            String[] selectionArgs;
            if (PicturePicker.getInstance().getPickPictureOptions().isSelectGif()) {
                selectionArgs = new String[]{"image/jpeg", "image/png", "image/jpg", "image/gif", scanFolderAbsPath};
            } else {
                selectionArgs = new String[]{"image/jpeg", "image/png", "image/jpg", scanFolderAbsPath};
            }

            loader.setSelectionArgs(selectionArgs);
        }
        //按加入时间降序排列
        loader.setSortOrder(MediaStore.Images.Media.DATE_ADDED + " DESC");
        return loader;
    }


    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {

        return createLoader();
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

        pictureFolderList.clear();

        if (data != null) {

            // new一个集合存储所有图片
            List<PictureItem> allPictureList = new ArrayList<>();

            //             图片目录路径和图片目录对象的映射
            ArrayMap<String, PictureFolder> path2PictureFolderMap = new ArrayMap<>();

            while (data.moveToNext()) {
                //得到图片的路径
                String picturePath = data.getString(data.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));

                if (!TextUtils.isEmpty(picturePath)) {
                    File pictureFile = new File(picturePath);
                    if (pictureFile.exists() && pictureFile.length() != 0) {

                        PictureItem pictureItem = new PictureItem();
                        pictureItem.pictureAbsPath = picturePath;
                        pictureItem.pictureAddTime = data.getLong(data.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_ADDED));
                        pictureItem.pictureHeight = data.getInt(data.getColumnIndexOrThrow(MediaStore.Images.Media.HEIGHT));
                        pictureItem.pictureWidth = data.getInt(data.getColumnIndexOrThrow(MediaStore.Images.Media.WIDTH));
                        pictureItem.pictureMimeType = data.getString(data.getColumnIndexOrThrow(MediaStore.Images.Media.MIME_TYPE));
                        pictureItem.pictureSize = data.getInt(data.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE));
                        pictureItem.pictureName = data.getString(data.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME));

                        //得到文件的父目录文件
                        File parentFile = pictureFile.getParentFile();

                        //得到父目录的路径并统一转为小写
                        String parentFilePath = parentFile.getAbsolutePath().toLowerCase();

                        //如果图片文件夹已经被存储
                        if (null != path2PictureFolderMap.get(parentFilePath)) {
                            path2PictureFolderMap.get(parentFilePath).pictureItemList.add(pictureItem);
                        } else {
                            PictureFolder pictureFolder = new PictureFolder();
                            pictureFolder.folderName = parentFile.getName();
                            pictureFolder.folderAbsPath = parentFilePath;
                            pictureFolder.pictureItemList = new ArrayList<>();
                            pictureFolder.pictureItemList.add(pictureItem);
                            //因为扫描时是按照加入时间降序排序，所以第一个图片为最新的，设为封面
                            pictureFolder.folderCover = pictureItem;
                            pictureFolderList.add(pictureFolder);
                            path2PictureFolderMap.put(parentFilePath, pictureFolder);
                        }

                        //把此图片加到“全部图片”里
                        allPictureList.add(pictureItem);
                    }
                }
            }

            if (allPictureList.size() > 0) {
                PictureFolder folder = new PictureFolder();
                folder.folderName = context.getResources().getString(R.string.all_pictures);
                folder.pictureItemList = allPictureList;
                folder.folderCover = allPictureList.get(0);
                pictureFolderList.add(0, folder);
            }

            if (scanFinishListener != null) {
                scanFinishListener.onScanFinish(pictureFolderList);
            }
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        loader.stopLoading();
    }
} public class Utils {

    public static int getScreenWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }


    /**
     * 设置屏幕透明度  0.0不透明 ~ 1.0透明
     * @param activity
     * @param alpha
     */
    public static void setActivityBackgroundAlpha(Activity activity, float alpha) {

//        activity. getWindow().getDecorView().setAlpha(alpha);
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = alpha;
        activity.getWindow().setAttributes(lp);

    }


    private static Toast toast;
    public static void showToast(Context context, String s) {
        if (toast == null) {
            toast = Toast.makeText(context.getApplicationContext(), s, Toast.LENGTH_SHORT);
        }
        toast.setText(s);
        toast.show();
    }

    /**
     * 获得状态栏的高度
     * @param context
     * @return
     */
    public static int getStatusHeight(Context context) {
        int statusHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
            statusHeight = context.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusHeight;
    }


    private static boolean hasSDCard() {
        return Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED) && !Environment.isExternalStorageRemovable();

    }

    public static String createTakePhotoFolderPath(Context context) {

        if (hasSDCard()) {
            File file = context.getExternalFilesDir(null);
            if (file != null) {
                return file.getAbsolutePath();
            }
        }
        return context.getFilesDir().getAbsolutePath();
    }

    public static String createTakePhotoPath(Context context) {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINESE).format(new Date());
        String pictureFileName = "JPEG_" + timeStamp + ".jpg";
        return new File(createTakePhotoFolderPath(context), pictureFileName).getAbsolutePath();
    }

    /**
     * 是否隐藏状态栏
     * @param activity
     * @param hide
     */
    public static void hideStatusBar(Activity activity,boolean hide) {
        if (hide) {
            //隐藏状态栏
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else {
            ////显示状态栏
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }

}
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.ListPopupWindow;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import devin.com.picturepicker.R;
import devin.com.picturepicker.adapter.PictureGridAdapter;
import devin.com.picturepicker.adapter.PopupFolderListAdapter;
import devin.com.picturepicker.adapter.viewholder.ItemPictureGridHolder;
import devin.com.picturepicker.constant.PreviewAction;
import devin.com.picturepicker.javabean.PictureFolder;
import devin.com.picturepicker.javabean.PictureItem;
import devin.com.picturepicker.options.PickOptions;
import devin.com.picturepicker.pick.PicturePicker;
import devin.com.picturepicker.pick.PictureScanner;
import devin.com.picturepicker.utils.Utils;
package devin.com.picturepicker.activity;





public class PictureGridActivity extends PictureBaseActivity implements View.OnClickListener, PicturePicker.OnPictureSelectedListener {

    public static final int RECYCLER_VIEW_COLUMN = 4;

    /**
     * 打开相机请求码
     */
    private final int OPEN_CAMERA_REQUEST_CODE = 1001;

    /**
     * 去预览并选择图片请求码
     */
    private final int PREVIEW_IMAGE_REQUEST_CODE = 1002;


    public static final String EXTRA_RESULT_PICK_IMAGES = "extra_result_pick_pictures";

    private String takePhotoPath;

    private PictureScanner pictureScanner;
    private PicturePicker picturePicker = PicturePicker.getInstance();

    private RecyclerView rvPictures;
    private LinearLayout llFootBar;
    private TextView btnImgFolder;
    private Button btnImgPreview;

    private PictureGridAdapter pictureGridAdapter;
    private List<PictureItem> currentPictureItemList = new ArrayList<>();

    private PopupFolderListAdapter folderListAdapter;
    private List<PictureFolder> pictureFolderList = new ArrayList<>();

    private ListPopupWindow listPopupWindow;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            takePhotoPath = savedInstanceState.getString("takePhotoPath");
            picturePicker.setPickPictureOptions((PickOptions) savedInstanceState.getSerializable("pickPictureOptions"));