如何使用日期时间戳对文件进行排序

时间:2014-03-28 06:00:53

标签: java android sorting android-sdcard

捕捉图片,然后存储到SD卡显示在列表中,但这里我需要一个小改动, 我仍然老了,最新到底 ,所以现在 我想在最上面显示最新图片 datetimestamp的基础,用作文件名的一部分。

UploadActivity.java 代码: -

 String fileName;

 static List <String> ImageList;

  /*** Get Images from SDCard ***/
    ImageList = getSD();

    // ListView and imageAdapter
    lstView = (ListView) findViewById(R.id.listView1);
    lstView.setAdapter(new ImageAdapter(this)); 
    }

    public static List <String> getSD()
    {
        List <String> it = new ArrayList <String>();
        String string = "/mnt/sdcard/Pictures/SamCam/";
        f = new File (string+ CameraLauncherActivity.folder+ "/");
        files = f.listFiles ();

        for (int i = 0; i < files.length; i++)
            {
                file = files[i];
                Log.d("Count",file.getPath());
                it.add (file.getPath());
            }
    return it;  
    }


    public class ImageAdapter extends BaseAdapter
    {
        private Context context;

            public ImageAdapter(Context c)
        {
            // TODO Auto-generated method stub
            context = c;

        }

注意:我在将图像存储到SD卡时使用日期/时间戳。

所以最后它看起来像这样:

  AU_20140328163947_1_4_X-1-4-006.jpg
以下格式

仍然列出文件,如下所示:

AU_20140328163947_1_4_X-1-4-006.jpg

 AU_20140328163948_1_4_X-1-4-007.jpg

 AU_20140328163949_1_4_X-1-4-008.jpg

但我希望以下列格式列出文件: -

AU_20140328163949_1_4_X-1-4-008.jpg

 AU_20140328163948_1_4_X-1-4-007.jpg

 AU_20140328163947_1_4_X-1-4-006.jpg

删除列表中图片的代码: -

// btnDelete
        final ImageButton btnDelete = (ImageButton) convertView.findViewById(R.id.btnDelete);
        btnDelete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

                    // set title
                    alertDialogBuilder.setTitle("Delete Image");

                    // Setting Icon to Dialog
                    alertDialogBuilder.setIcon(R.drawable.ic_launcher);

                    // set dialog message
                    alertDialogBuilder
                        .setMessage("Are you sure you want to delete this image?")
                        .setCancelable(false)
                        .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                // if this button is clicked, close
                                // current activity
                                // to get fileName
                                fileName = ImageList.get(position).toString().substring(strPath.lastIndexOf('/')+1, strPath.length());
                                // to get SD card path (Folders+fileName)
                                 String fileToDelete = Environment.getExternalStorageDirectory().getPath() +"/Pictures/SamCam/" + CameraLauncherActivity.folder+ "/" + fileName;
                                 Log.d("FileToDelete", fileToDelete);
                                  File myFile = new File(fileToDelete);
                                  // if image exists
                                      if(myFile.exists())
                                          // delete image
                                        myFile.delete();
                                      // get position and delete
                                      ImageList.remove(position);
                                      // to refresh the view
                                 ((BaseAdapter) lstView.getAdapter()).notifyDataSetChanged(); 

                                 dialog.cancel();
                            }
                          })
                        .setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                // if this button is clicked, just close
                                // the dialog box and do nothing
                                dialog.cancel();
                            }
                        });

                        // create alert dialog
                        AlertDialog alertDialog = alertDialogBuilder.create();

                        // show it
                        alertDialog.show();
                // TODO Auto-generated method stub

            }
        });

        return convertView;

            }   
        }

3 个答案:

答案 0 :(得分:1)

如果以相反的顺序获取数据,则可以使用反向循环。

尝试以下循环

for (int i = files.length-1; i >= 0; i--)
{
file = files[i];
Log.d("Count",file.getPath());
it.add (file.getPath());
}

而不是

 for (int i = 0; i < files.length; i++)
 {
      file = files[i];
      Log.d("Count",file.getPath());
      it.add (file.getPath());
  }

或使用特定字段对数据进行排序

在使用for循环之前对数组数据进行排序并使用相同的循环..

Arrays.sort(files, new Comparator<Object>()
{
    public int compare(Object o1, Object o2) {

        if (((File)o1).lastModified() > ((File)o2).lastModified()) {
            return -1;
        } else if (((File)o1).lastModified() < ((File)o2).lastModified()) {
            return +1;
        } else {
            return 0;
        }
    }

});

for (int i = 0; i < files.length; i++)
 {
      file = files[i];
      Log.d("Count",file.getPath());
      it.add (file.getPath());
  }

答案 1 :(得分:0)

将MediaStore ContentProvider用于此作业 使用此method

使用MediaStore保存图像

您可以使用此method

查询图片

按MediaStore.Images.Media.DATE_TAKEN

设置顺序

答案 2 :(得分:0)

您可以使用列表适配器中的Collections.sort方法根据图像文件名对值进行排序,如:

Collections.sort(ImageList, new Comparator<String>() {
  int compare(String obj1, String obj2) {
        return obj1.compareTo(obj2);
  }
});

compareTo方法以及compareToIgnoreCase方法,使用您认为合适的方法,并且,您可以尝试使用obj1和obj2,也就是说,您可以将条件交换为:

 return obj2.compareTo(obj1);

这样你的列表就会被排序。希望有所帮助!

修改

既然您知道格式为_然后是-.jpg,那么您可以做的是在比较器中将值分开 - 如:

Collections.sort(ImageList, new Comparator<String>() {
  int compare(String obj1, String obj2) {
        String[] obj1Arr = obj1.split(-);
        String[] obj2Arr = obj2.split(-);

        obj1Arr = obj1Arr[1].split("."); // to just get the counter value
        obj2Arr = obj2Arr[1].split(".");

        return obj1Arr[0].compareTo(obj2Arr[0]);
  }
});