Android画廊专辑图像按日期添加添加

时间:2017-09-04 05:40:11

标签: java android android-gallery galleryview

AlbumView sorted perfectly AlbumallImages Unsorted.

从过去的几天开始,我正在制作像whatsapp这样的Gallery视图, 并且还在努力。

我已成功创建了专辑视图以及该特定专辑中的图像。 主要问题在于对相册文件夹下的图像进行排序。 我试图根据最后添加的图像显示图像。 但图像以ASC顺序显示而不是DESC。

坚持这个。提供代码,请指出问题,如果有的话。 提前谢谢。

MainActivity(AlbumView)的.java

public class MainActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private RecyclerView mRecyclerView;
    private GalleryAlbumAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private ArrayList<AlbumsModel> albumsModels;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actitivy_gallery_albums);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            getSupportActionBar().setTitle("Album Thumbnail Images");
        }
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);
        // use a linear layout manager
        mRecyclerView.setLayoutManager(new GridLayoutManager(this,3));
        // create an Object for Adapter
        mAdapter = new GalleryAlbumAdapter (MainActivity.this,getGalleryAlbumImages ());
        // set the adapter object to the Recyclerview
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.SetOnItemClickListener(new GalleryAlbumAdapter.OnItemClickListener ()
        {
            @Override
            public void onItemClick (View v, int position) {
                // do something with position
                Intent galleryAlbumsIntent = new Intent(MainActivity.
                        this,ShowAlbumImagesActivity.class);
                galleryAlbumsIntent.putExtra ("position",position);
                galleryAlbumsIntent.putExtra ("albumsList", getGalleryAlbumImages());
                startActivity (galleryAlbumsIntent);
            }
        });
    }
    private ArrayList<AlbumsModel> getGalleryAlbumImages() {
        final String[] columns = { MediaStore.Images.Media.DATA,
                MediaStore.Images.Media._ID ,MediaStore.Images.Media.DATE_ADDED};
        final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
        Cursor imagecursor = this.getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
                null, null, orderBy + " DESC");
        albumsModels = Utils.getAllDirectoriesWithImages(imagecursor);
        return albumsModels;
    }
} 

ShowAlbumImagesActivity.java

public class ShowAlbumImagesActivity extends AppCompatActivity implements
        ShowAlbumImagesAdapter.ViewHolder.ClickListener {
    private Toolbar toolbar;
    private RecyclerView mRecyclerView;
    private ShowAlbumImagesAdapter mAdapter;
    private Button btnSelection;
    private ArrayList<AlbumsModel> albumsModels;
    private int mPosition;
    public ArrayList<Uri> mShareImages = new ArrayList<Uri>();
    LinearLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_gallery_images);
        //setContentView(R.layout.activity_adapter_sample);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        btnSelection = (Button) findViewById(R.id.btnShow);
        layout = (LinearLayout) findViewById(R.id.imgView);

        if (toolbar != null) {
            setSupportActionBar(toolbar);
            getSupportActionBar().setTitle("Album Images");
        }
        mPosition = (int) getIntent().getIntExtra("position", 0);
        albumsModels = (ArrayList<AlbumsModel>) getIntent().getSerializableExtra("albumsList");
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);
        // use a linear layout manager
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        // create an Object for Adapter
        mAdapter = new ShowAlbumImagesAdapter(ShowAlbumImagesActivity.this, getAlbumImages(), this);
        // set the adapter object to the Recyclerview
        mRecyclerView.setAdapter(mAdapter);
        btnSelection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (Uri uri : mShareImages) {
                    String uriString = uri.toString();
                    Uri imgUri = Uri.parse(uriString);
                    Log.e("img uri:", uriString);
                    ImageView image = new ImageView(ShowAlbumImagesActivity.this);
                    image.setLayoutParams(new android.view.ViewGroup.LayoutParams(160, 160));
                    image.setMaxHeight(40);
                    image.setMaxWidth(40);
                    //image.setImageURI(null);
                    image.setImageURI(imgUri);
                    // Adds the view to the layout
                    layout.addView(image);
                    //imgView1.setImageURI(null);
                    //imgView1.setImageURI(imgUri);
                    // do whatever you want with your string
                }
            }
        });
    }

    private Uri getImageContentUri(File imageFile) {
        String filePath = imageFile.getAbsolutePath();
        final String[] columns = {MediaStore.Images.Media.DATA,
                MediaStore.Images.Media._ID};
        final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
        Cursor cursor = this.getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[]{MediaStore.Images.Media._ID},
                MediaStore.Images.Media.DATA + "=? ",
                new String[]{filePath}, orderBy + " DESC");
        ///orderBy+"DESC"
        if (cursor != null && cursor.moveToFirst()) {
            int id = cursor.getInt(cursor
                    .getColumnIndex(MediaStore.MediaColumns._ID));
            Uri baseUri = Uri.parse("content://media/external/images/media");
            return Uri.withAppendedPath(baseUri, "" + id);
        } else {
            if (imageFile.exists()) {
                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.DATA, filePath);
                return this.getContentResolver().insert(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            } else {
                return null;
            }
        }
    }

    private ArrayList<AlbumImages> getAlbumImages() {
        Object[] abc = albumsModels.get(mPosition).folderImages.toArray();
        Log.i("imagesLength", "" + abc.length);
        ArrayList<AlbumImages> paths = new ArrayList<AlbumImages>();
        int size = abc.length;
        for (int i = 0; i < size; i++) {
            AlbumImages albumImages = new AlbumImages();
            albumImages.setAlbumImages((String) abc[i]);
            paths.add(albumImages);
        }
        return paths;
    }

    @Override
    public void onItemClicked(int position) {
        if (mShareImages.size() > 0) {
            toggleSelection(position);
        } else {
            intent(position);
        }

    }

    @Override
    public boolean onItemLongClicked(int position) {
        toggleSelection(position);
        return true;
    }

    public void toggleSelection(int position) {
        mAdapter.toggleSelection(position);
        int count = mAdapter.getSelectedItemCount();
        Log.i("string path", "" + mAdapter.getAlbumImagesList().get(position).getAlbumImages());
        Uri uriPath = Uri.parse(mAdapter.getAlbumImagesList().get(position).getAlbumImages());
        String path = uriPath.getPath();
        File imageFile = new File(path);
        Uri uri = getImageContentUri(imageFile);
        if (mAdapter.isSelected(position)) {
            mShareImages.add(uri);
        } else {
            mShareImages.remove(uri);
        }
        Log.i("uri path", "" + mShareImages);
    }

    public void intent(int mPosition) {
        Uri uriPath = Uri.parse(mAdapter.getAlbumImagesList().get(mPosition).getAlbumImages());
        String path = uriPath.getPath();
        File imageFile = new File(path);
        Uri uri = getImageContentUri(imageFile);
        if (uri != null) {
            Intent i = new Intent(this, SelectedImagePreview.class);
            i.setData(Uri.parse(path));
            startActivity(i);
        }
        Log.e("Selected Image path", "" + path);
    }
}

ShowAlbumImagesAdapter.java

public class ShowAlbumImagesAdapter extends SelectableAdapter<ShowAlbumImagesAdapter.ViewHolder> {
    public Context mContext;
    private ArrayList<AlbumImages> mAlbumImages;
    public boolean mShowCheckBox;
    private ViewHolder.ClickListener clickListener;
    //    public ArrayList<Uri> mShareImages = new ArrayList<Uri> ();
    public ShowAlbumImagesAdapter (Context context, ArrayList<AlbumImages>
            galleryImagesList,ViewHolder.ClickListener clickListener) {
        this.mAlbumImages = galleryImagesList;
        this.mContext = context;
        this.clickListener = clickListener;
    }
    // Create new views
    @Override
    public ShowAlbumImagesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                                int viewType) {
        // create a new view
        View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.row_show_gallery_images, null);
        ViewHolder viewHolder = new ViewHolder(itemLayoutView,clickListener);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, int position) {
        final int pos = position;
        Glide.with (mContext)
                .load("file://"+mAlbumImages.get (position).getAlbumImages ())
                .centerCrop()
                .placeholder(R.drawable.image_loading)
                .crossFade()
                .into (viewHolder.imgAlbum);
        viewHolder.selectedOverlay.setVisibility(isSelected(position) ? View.VISIBLE : View.INVISIBLE);
    }
    // Return the size arraylist
    @Override
    public int getItemCount() {
        return mAlbumImages.size();
    }
    public static class ViewHolder extends RecyclerView.ViewHolder implements
            View.OnClickListener,View.OnLongClickListener {
        private final RelativeLayout selectedOverlay;
        public ImageView imgAlbum;
        public AlbumsModel singleItem;
        private ClickListener listener;
        public ViewHolder(View itemLayoutView,ClickListener listener) {
            super(itemLayoutView);
            this.listener = listener;
            imgAlbum = (ImageView) itemLayoutView.findViewById(R.id.img_album);
            selectedOverlay = (RelativeLayout) itemView.findViewById(R.id.selected_overlay);
            itemLayoutView.setOnClickListener(this);
            itemLayoutView.setOnLongClickListener (this);
        }
        @Override
        public void onClick(View v) {
            if (listener != null) {
                listener.onItemClicked(getAdapterPosition ());
            }
        }
        @Override
        public boolean onLongClick (View view) {
            if (listener != null) {
                return listener.onItemLongClicked(getAdapterPosition ());
            }
            return false;
        }
        public interface ClickListener {
            public void onItemClicked(int position);
            public boolean onItemLongClicked(int position);
        }
    }
    // method to access in activity after updating selection
    public ArrayList<AlbumImages> getAlbumImagesList() {
        return mAlbumImages;
    }
} 

AlbumImages.java

public class AlbumImages {
    protected String albumImages;
    private boolean isSelected;
    public boolean isSelected () {
        return isSelected;
    }
    public void setSelected (boolean isSelected) {
        this.isSelected = isSelected;
    }
    public String getAlbumImages () {
        return albumImages;
    }
    public void setAlbumImages (String albumImages) {
        this.albumImages = albumImages;
    }
} 

Utils.java

      public class Utils {

    public static ArrayList<AlbumsModel> getAllDirectoriesWithImages(
            Cursor cursor) {
        if (cursor == null) {
            return null;
        }
        cursor.moveToFirst();
        int size = cursor.getCount();
        TreeSet<String> folderPathList = new TreeSet<String>();
        ArrayList<AlbumsModel> albumsModels = new ArrayList<AlbumsModel>();
        HashMap<String, AlbumsModel> map = new HashMap<String, AlbumsModel>();
        String imgPath, folderPath;
        AlbumsModel tempAlbumsModel;
        for (int i = 0; i < size; i++) {
            imgPath = cursor.getString(0).trim();
            folderPath = imgPath.substring(0, imgPath.lastIndexOf("/"));
            if (folderPathList.add(folderPath)) {
                AlbumsModel gm = new AlbumsModel();
                String folderName = gm.getFolderName();
                String folderImagePath = gm.getFolderName();
                gm.folderName = folderPath.substring(
                        folderPath.lastIndexOf("/") + 1, folderPath.length());
                gm.folderImages.add(imgPath);
                gm.folderImagePath = imgPath;
                albumsModels.add(gm);
                map.put(folderPath, gm);
            } else if (folderPathList.contains(folderPath)) {
                tempAlbumsModel = map.get(folderPath);
                tempAlbumsModel.folderImages.add(imgPath);
            }
            cursor.moveToNext();
        }
        return albumsModels;
    } 

0 个答案:

没有答案