如何按日期制作排序图库缩略图

时间:2015-04-20 10:36:17

标签: android image gallery

我正在开发一个Android应用程序。此应用程序从库中获取所有缩略图图像。我想按日期对这些缩略图进行排序,但我不能这样做。

请帮帮我。

获取所有图片

// Set up an array of the Thumbnail Image ID column we want
String[] columns = {MediaStore.Images.Media._ID};

String orderBy = MediaStore.Images.Thumbnails._ID + " DESC LIMIT 10";

// Create the cursor pointing to the SDCard

cursor = getActivity().managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
        columns, // Which columns to return
        null,       // Return all rows
        null,
        orderBy);

// Get the column index of the Thumbnails Image ID
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

myGalleryImages = (GridView) view.findViewById(R.id.my_gallery);
myGalleryImages.setAdapter(new ImageAdapter(getActivity()));

设置图片

    // Move cursor to current position
    cursor.moveToPosition(position);
    // Get the current value for the requested column
    int imageID = cursor.getInt(columnIndex);
    // Set the content of the image based on the provided URI
    holder.image.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));

2 个答案:

答案 0 :(得分:7)

像这样更新columnsorderBy

  String[] columns = {MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.DATE_TAKEN};

  String orderBy = MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC"; 

看看是否有帮助。

您还可以获取真实图像而不是缩略图,并使用图像加载库来处理正确的重新调整大小。在这种情况下,请将Thumbnails引用替换为ImageColumns

答案 1 :(得分:3)

此代码将保存前100个拇指(按日期排序)

      Cursor cursor = this.getContentResolver().query( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                projection, // Which columns to return
                null,       // Return all rows
                null,
                "image_id DESC");

// Get the column index of the Thumbnails Image ID
        int columnIndex = cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA);
        for(int i =0;i<cursor.getCount();i++){
            if (i==100) break;
            cursor.moveToPosition(i);
            mImagesFromGallery[i] = cursor.getString(columnIndex);
        }
        cursor.close();