如何使用datetimestamp对文件进行排序

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

UploadActivity.java代码: –

String fileName; static List  ImageList; /*** Get Images from SDCard ***/ ImageList = getSD(); // ListView and imageAdapter lstView = (ListView) findViewById(R.id.listView1); lstView.setAdapter(new ImageAdapter(this)); } public static List  getSD() { List  it = new ArrayList (); 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; } } 

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

尝试以下循环

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

将MediaStore ContentProvider用于此作业使用此方法使用MediaStore保存图像

您可以使用此方法查询图像

通过MediaStore.Images.Media.DATE_TAKEN设置订单

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

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

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

  return obj2.compareTo(obj1); 

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

编辑:

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

 Collections.sort(ImageList, new Comparator() { 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]); } });