LoaderManager 备份

@bingoogolapple 2017-01-10 15:34:15发表于 bingoogolapple/bingoogolapple.github.io AndroidDatabase

public class BGALoadPhotoHelper {

    public static void initLoader(AppCompatActivity activity, Delegate delegate, boolean takePhotoEnabled) {
        activity.getSupportLoaderManager().initLoader(0, null, new BGAImageFolderLoaderCallbacks(activity, delegate, takePhotoEnabled));
    }

    public static void destroyLoader(AppCompatActivity activity) {
        activity.getSupportLoaderManager().destroyLoader(0);
    }

    private static boolean isImageFile(String path) {
        if (TextUtils.isEmpty(path)) {
            return false;
        }
        
        File file = new File(path);
        return file.exists() && file.length() > 0;

        // 获取图片的宽和高,但不把图片加载到内存中
//        BitmapFactory.Options options = new BitmapFactory.Options();
//        options.inJustDecodeBounds = true;
//        BitmapFactory.decodeFile(path, options);
//        return options.outMimeType != null;
    }

    public static class BGAImageFolderLoaderCallbacks implements LoaderManager.LoaderCallbacks<Cursor> {
        private AppCompatActivity mActivity;
        private boolean mTakePhotoEnabled;
        private Delegate mDelegate;

        public BGAImageFolderLoaderCallbacks(AppCompatActivity activity, Delegate delegate, boolean takePhotoEnabled) {
            mActivity = activity;
            mDelegate = delegate;
            mTakePhotoEnabled = takePhotoEnabled;
        }

        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            return new BGAImageFolderLoader(mActivity);
        }

        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
            ArrayList<BGAImageFolderModel> imageFolderModels = new ArrayList<>();

            BGAImageFolderModel allImageFolderModel = new BGAImageFolderModel(mTakePhotoEnabled);
            allImageFolderModel.name = mActivity.getString(R.string.bga_pp_all_image);
            imageFolderModels.add(allImageFolderModel);

            HashMap<String, BGAImageFolderModel> imageFolderModelMap = new HashMap<>();

            try {
                BGAImageFolderModel otherImageFolderModel;
                if (cursor != null && cursor.getCount() > 0) {
                    boolean firstInto = true;
                    while (cursor.moveToNext()) {
                        String imagePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                        if (!isImageFile(imagePath)) {
                            continue;
                        }

                        if (firstInto) {
                            allImageFolderModel.coverPath = imagePath;
                            firstInto = false;
                        }
                        // 所有图片目录每次都添加
                        allImageFolderModel.addLastImage(imagePath);

                        String folderPath = null;
                        // 其他图片目录
                        File folder = new File(imagePath).getParentFile();
                        if (folder != null) {
                            folderPath = folder.getAbsolutePath();
                        }

                        if (TextUtils.isEmpty(folderPath)) {
                            int end = imagePath.lastIndexOf(File.separator);
                            if (end != -1) {
                                folderPath = imagePath.substring(0, end);
                            }
                        }

                        if (!TextUtils.isEmpty(folderPath)) {
                            if (imageFolderModelMap.containsKey(folderPath)) {
                                otherImageFolderModel = imageFolderModelMap.get(folderPath);
                            } else {
                                String folderName = folderPath.substring(folderPath.lastIndexOf(File.separator) + 1);
                                if (TextUtils.isEmpty(folderName)) {
                                    folderName = "/";
                                }
                                otherImageFolderModel = new BGAImageFolderModel(folderName, imagePath);
                                imageFolderModelMap.put(folderPath, otherImageFolderModel);
                            }
                            otherImageFolderModel.addLastImage(imagePath);
                        }
                    }

                    // 添加其他图片目录
                    imageFolderModels.addAll(imageFolderModelMap.values());
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }

            mDelegate.onLoadedPhoto(imageFolderModels);
        }

        @Override
        public void onLoaderReset(Loader<Cursor> loader) {
        }
    }

    public static class BGAImageFolderLoader extends CursorLoader {

        public BGAImageFolderLoader(Context context) {
            super(context);

            setUri(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            setProjection(new String[]{MediaStore.Images.Media.DATA});
            setSelection(MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=?");
            setSelectionArgs(new String[]{"image/jpeg", "image/png"});
            setSortOrder(MediaStore.Images.Media.DATE_ADDED + " DESC");
        }
    }

    public interface Delegate {

        void onLoadedPhoto(ArrayList<BGAImageFolderModel> imageFolderModels);
    }
}