Effect:

1. Entity class photo.ajva

public class Photo {
    private String name;/ / name
    private String date;/ / date
    private long size;  / / size
    private String path;/ / path
    /** * constructor */
    public Photo(a) {}public Photo(String name, String date, long size, String path) {
        this.name = name;
        this.date = date;
        this.size = size;
        this.path = path; }}Copy the code

2. Add permissions to androidmanifest.xml

    <! Read/write permission -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Copy the code

3. Read pictures from the phone in the Activity

    private void initData(a) {
        // Read the photos in the phone
        Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null.null.null.null);
         List<Photo> mPhotoList= new ArrayList<Photo>();
        while (cursor.moveToNext()) {
            // The path to the image
            String path=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            if(path! =null && path.length() >0) {
                // Get the name of the image
                String name = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
                // Get the date when the image was last modified
                File file = new File(path);
                long modifieTime = file.lastModified();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                String date = sdf.format(new Date(modifieTime));
                // Get the size of the image
                long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.SIZE));
                Photo photo = new Photo(name, date, size, path);
                mPhotoList.add(photo);
            }
        }
        mPhotoList = sortList(mPhotoList);
    }
   /** * List in descending order by time *@param L
     * @return* /
    private List<Photo> sortList(List<Photo> L){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Photo temp = new Photo();
        // Bubble sort, large time at the front of the array
        for(int i=0; i<L.size()-1; i++){
            for(int j=i+1; j<L.size(); j++){ String date1=L.get(i).getDate(); String date2=L.get(j).getDate(); Date d1=sdf.parse(date1,new ParsePosition(0));
                Date d2=sdf.parse(date2,new ParsePosition(0));
                boolean flag = d1.before(d2);
                //flag=true for descending order, flag=flase for ascending order
                if(flag){ temp = L.get(i); L.set(i, L.get(j)); L.set(j, temp); }}}return L;
    }
Copy the code