2016-06-11 29 views
0

中的日期對SDCard圖像進行排序我正在嘗試將基於日期的sdcard圖像排序爲gridview。我嘗試了各種代碼。但我無法做到這一點。請給我一些幫助。如何根據android

public class Sdcard extends Activity { 

// Cursor used to access the results from querying for images on the SD card. 

private Cursor cursor; 

// Column index for the Thumbnails Image IDs. 

private int columnIndex; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_sdcard); 

    // Set up an array of the Thumbnail Image ID column we want 
    String[] projection = {MediaStore.Images.Thumbnails._ID}; 
    // Create the cursor pointing to the SDCard 
    cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, 
      projection, // Which columns to return 
      null,  // Return all rows 
      null, 
      MediaStore.Images.Thumbnails.IMAGE_ID); 
    // Get the column index of the Thumbnails Image ID 
    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID); 

    GridView sdcardImages = (GridView) findViewById(R.id.gridView1); 
    sdcardImages.setAdapter(new ImageAdapter(this)); 


} 

//圖片適配器將圖像鏈接到GridView

private class ImageAdapter extends BaseAdapter { 

    private Context context; 

    public ImageAdapter(Context localContext) { 
     context = localContext; 
    } 

    public int getCount() { 
     return cursor.getCount(); 
    } 
    public Object getItem(int position) { 
     return position; 
    } 
    public long getItemId(int position) { 
     return position; 
    } 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView picturesView; 
     if (convertView == null) { 
      picturesView = new ImageView(context); 
      // 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 
      picturesView.setImageURI(Uri.withAppendedPath(
        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID)); 
      picturesView.setScaleType(ImageView.ScaleType.FIT_XY); 
      picturesView.setPadding(10, 10, 10, 10); 
      picturesView.setLayoutParams(new GridView.LayoutParams(100, 100)); 
     } 
     else { 
      picturesView = (ImageView)convertView; 
     } 
     return picturesView; 
    } 
} 

}

Thius代碼是工作在模擬器細和在移動不工作。

回答

0

一個簡單的解決方案,以在上次更改日期從路徑基本文件排序

File sd = new File(**pathofimagesfiles**); 
    File[] sdFileList = sd.listFiles(); 
    if (sdFileList != null && sdFileList.length > 1) { 
     Arrays.sort(sdFileList, new Comparator<File>() { 
      @Override 
      public int compare(File object1, File object2) { 
       return object1.lastModified() - object2.lastModified(); 
      } 
     }); 
    } 

這可以幫助!

+0

嗨,我是新來這個概念。你能給我完整的編碼嗎? – bala

+0

@bala你需要什麼? – Riskhan

+0

我想從手機和SD卡上獲取所有圖像。所有這些圖像應該按日期明智地排列。你能否給我提供樣品編碼 – bala

0

使用可以使用下面的代碼。爲我工作。對於降序

final File[] files= file.listFiles(); 
if (files != null && files.length > 1) { 
      Collections.sort(Arrays.asList(files), new Comparator<File>() { 
       public int compare(File o1, File o2) { 
        long lastModifiedO1 = o1.lastModified(); 
        long lastModifiedO2 = o2.lastModified(); 

        return (lastModifiedO2 < lastModifiedO1) ? -1 : ((lastModifiedO1 > lastModifiedO2) ? 1 : 0); 
       } 
      }); 
     } 

爲升序 參考, https://stackoverflow.com/a/20065704/8416317