Search in sources :

Example 1 with MediaBean

use of bean.MediaBean in project GomoTest by suReZj.

the class SelectActivity method getData.

public void getData() {
    final List<AlbumBean> list = new ArrayList<>();
    allPhotosTemp = new HashMap<>();
    albumName = new ArrayList<>();
    new Thread(new Runnable() {

        @Override
        public void run() {
            String[] projImage = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA, MediaStore.Images.Media.SIZE, MediaStore.Images.Media.DISPLAY_NAME };
            Cursor mCursor = getContentResolver().query(mImageUri, projImage, MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=?", new String[] { "image/jpeg", "image/png", "image/jpg", "image/bmp" }, MediaStore.Images.Media.DATE_MODIFIED + " desc");
            if (mCursor != null) {
                while (mCursor.moveToNext()) {
                    // 获取图片的路径
                    String path = mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA));
                    int size = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Images.Media.SIZE)) / 1024;
                    String displayName = mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
                    // 用于展示相册初始化界面
                    mediaBeen.add(new MediaBean(path, size, displayName));
                    // 获取该图片的父路径名
                    String dirPath = new File(path).getParentFile().getAbsolutePath();
                    // 存储对应关系
                    AlbumBean bean = new AlbumBean();
                    if (allPhotosTemp.containsKey(dirPath)) {
                        List<MediaBean> data = allPhotosTemp.get(dirPath);
                        data.add(new MediaBean(path, size, displayName));
                        bean.setAlbumName(dirPath);
                        bean.setPath(path);
                        list.add(bean);
                        continue;
                    } else {
                        albumName.add(dirPath);
                        List<MediaBean> data = new ArrayList<>();
                        data.add(new MediaBean(path, size, displayName));
                        allPhotosTemp.put(dirPath, data);
                        bean.setAlbumName(dirPath);
                        bean.setPath(path);
                        list.add(bean);
                    }
                }
                mCursor.close();
            }
            // 更新界面
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    adapter = new select_recycle_adapter(allPhotosTemp, albumName);
                    recyclerView.setAdapter(adapter);
                    recyclerView.setLayoutManager(new LinearLayoutManager(SelectActivity.this));
                    DataSupport.deleteAll(AlbumBean.class);
                    DataSupport.saveAllAsync(list).listen(new SaveCallback() {

                        @Override
                        public void onFinish(boolean success) {
                            if (flag) {
                                EventBus.getDefault().post(event);
                                flag = false;
                            }
                        }
                    });
                    adapter.setOnItemClickLitener(new select_recycle_adapter.OnItemClickLitener() {

                        @Override
                        public void onItemClick(View view, int position) {
                            Intent intent = new Intent(SelectActivity.this, AlbumActivity.class);
                            String name = albumName.get(position);
                            intent.putExtra("name", name);
                            startActivity(intent);
                        }

                        @Override
                        public void onItemLongClick(View view, int position) {
                        }
                    });
                }
            });
        }
    }).start();
}
Also used : ArrayList(java.util.ArrayList) MediaBean(bean.MediaBean) Intent(android.content.Intent) Cursor(android.database.Cursor) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) adapter.select_recycle_adapter(adapter.select_recycle_adapter) SaveCallback(org.litepal.crud.callback.SaveCallback) AlbumBean(bean.AlbumBean) File(java.io.File)

Aggregations

adapter.select_recycle_adapter (adapter.select_recycle_adapter)1 Intent (android.content.Intent)1 Cursor (android.database.Cursor)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 TextView (android.widget.TextView)1 AlbumBean (bean.AlbumBean)1 MediaBean (bean.MediaBean)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 SaveCallback (org.litepal.crud.callback.SaveCallback)1