如何从上个小时拍摄的手机中加载图像?

时间:2016-04-19 14:11:18

标签: android android-image android-cursor android-contentresolver

我想加载并显示我的手机在过去一小时内拍摄的所有图像。我正在使用Content Resolver查询和一些参数来获取图像列表。我可以用它来显示最后10张图片:

String sortOrder = String.format("%s limit 50 ", MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");

这是使用下面的代码,但我想修改我的光标查询,只显示在最后一小时拍摄的图像。

以下是代码:

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

    //Current time in millis and hour ago
    int now = Calendar.getInstance().get(Calendar.MILLISECOND);
    long hourAgo = now - 60*60000;


    String[] columns = new String[]{
            MediaStore.Images.Media._ID,  // get the image
    };

    ContentResolver contentResolver = getContentResolver();


    // 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 = contentResolver.query(MediaStore.Images.Media.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.Media._ID);

我如何才能获取上一小时拍摄的图像列表?

我也尝试写这个,但它不起作用:

    final String[] selectionArgs = { MediaStore.Images.ImageColumns.DATE_TAKEN + "<"+ Long.toString(hourAgo) };
    cursor = contentResolver.query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            cols, // Which columns to return
            null,       // Return all rows
            selectionArgs ,
            orderBy);

0 个答案:

没有答案