【问题标题】:How to sort SDCard image based on date in android如何在android中根据日期对SDCard图像进行排序
【发布时间】:2017-09-11 15:22:09
【问题描述】:

我正在尝试根据日期将 sdcard 图像排序到 gridview 中。我尝试了各种代码。但我无法做到这一点。请给我一些帮助。

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 代码在模拟器中运行良好,但在移动设备中无法运行。

【问题讨论】:

    标签: android sorting gridview


    【解决方案1】:

    根据上次修改日期从路径对文件进行排序的简单解决方案

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

    这可能会有所帮助!

    【讨论】:

    • 嗨,我是这个概念的新手。你能给我完整的编码吗?
    • @bala 你需要什么?
    • 我想从手机和 sd 卡中获取所有图像。所有这些图像都应按日期排列。你能给我提供示例编码吗
    【解决方案2】:

    使用可以使用下面的代码。为我工作。降序

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多