有没有办法在图库android中按日期排序图像

时间:2012-03-30 01:08:58

标签: android android-gallery android-file android-cursor

我在Android应用中有自定义相机和图库视图。我的相机在我的图库中成功显示的特定文件夹中保存带有时间戳的图片。它似乎默认从最旧到最新或按字母顺序排序这些图像,不确定..但我希望它们以相反的顺序显示..有人能指出我正确的方向吗?可能吗?如何更改我的BaseAdapter或我的画廊活动的创建...

我的画廊活动:

public class GalleryView extends Activity{

    ImageView imageView;
    Cursor cursor;
    private int columnIndex;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery);
        Gallery ga = (Gallery)findViewById(R.id.Gallery01);
        // Set up an array of the Thumbnail Image ID column we want
        String[] projection = {MediaStore.Images.Media._ID};
        // Create the cursor pointing to the SDCard
        cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection, // Which columns to return
                MediaStore.Images.Media.DATA + " like ? ",
                new String[] {"%LC/images%"},  
                null);
        // Get the column index of the Thumbnails Image ID
        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);

        ga.setAdapter(new GallImageAdapter(this,cursor,columnIndex));



    }



}

我的自定义适配器:

public class GallImageAdapter extends BaseAdapter {
    public Cursor cursor;
    private int columnIndex;
    private Context context;
    int imageBackground;

    public GallImageAdapter(Context ctx, Cursor cur, int cIn) {
        context = ctx;
        columnIndex = cIn;
        cursor = cur;
    }

    @Override
    public int getCount() {

        return cursor.getCount();
    }

    @Override
    public Object getItem(int position) {

        return position;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v;
        if(convertView ==null){
            v = LayoutInflater.from(context).inflate(R.layout.galitem, parent, false);
        }else{
            v = convertView;
        }
        ImageView photo = (ImageView) v.findViewById(R.id.imageView);
        ImageView border = (ImageView) v.findViewById(R.id.borderView);
        ImageView d = (ImageView) v.findViewById(R.id.delView);

        // Move cursor to current position
        cursor.moveToPosition(position);
        // Get the current value for the requested column
        int imageID = cursor.getInt(columnIndex);
        // obtain the image URI
        Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
        String url = uri.toString();
        // Set the content of the image based on the image URI
        int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
        Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(),
                        originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
        photo.setImageBitmap(b);

        photo.setScaleType(ImageView.ScaleType.FIT_CENTER); 

        return v;
    }

}

1 个答案:

答案 0 :(得分:0)

尝试这个,让我知道它是否有效

final String orderBy = MediaStore.Images.Media._ID;

cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            projection, // Which columns to return
            MediaStore.Images.Media.DATA + " like ? ",
            new String[] {"%LC/images%"},  
            orderBy + " DESC");