2012-03-30 59 views
1

我在我的android應用程序中有一個工作的自定義相機和圖庫視圖。我的照相機將圖片與時間戳保存在一個特定的文件夾中,我已成功顯示在我的畫廊中。它似乎將這些圖像默認排序從最舊到最新或按字母順序排列,不確定..但我希望它們以相反的順序顯示..有人可以指引我朝着正確的方向嗎?可能嗎?改變我的BaseAdapter或我的我的畫廊活動的OnCreate ...有沒有一種方法來在圖庫中按日期排序圖像android

我的畫廊活動:

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; 
    } 

} 

回答

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"); 
相關問題