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

Java 如何使用datetimestamp对文件进行排序,java,android,sorting,android-sdcard,Java,Android,Sorting,Android Sdcard,我正在拍摄图像,然后存储到SD卡中并显示在列表中,但这里我需要一点小小的改变,我仍然在顶部变老,最新在底部变老,因此,现在我想在顶部显示最新的图片,该图片基于datetimestamp,用作文件名的一部分 UploadActivity.java代码:- String fileName; static List <String> ImageList; /*** Get Images from SDCard ***/ ImageList = getSD();

我正在拍摄图像,然后存储到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;

        }
仍以以下格式列出文件,如下所示:

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;

            }   
        }

为此作业使用MediaStore ContentProvider 使用以下命令使用MediaStore保存图像

你可以用这个查询图像


将顺序设置为MediaStore.Images.Media.DATE\u take

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

Collections.sort(ImageList, new Comparator<String>() {
  int compare(String obj1, String obj2) {
        return obj1.compareTo(obj2);
  }
});
这样,您的列表将被排序。希望有帮助

编辑:

因为您知道格式是u,然后是-.jpg,所以您可以在比较器中将值从-like中拆分出来:

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]);
  }
});
Collections.sort(ImageList,新比较器(){
int比较(字符串obj1、字符串obj2){
字符串[]obj1Arr=obj1.split(-);
字符串[]obj2Arr=obj2.split(-);
obj1Arr=obj1Arr[1]。拆分(“.”;//仅获取计数器值
obj2Arr=obj2Arr[1]。拆分(“.”);
返回obj1Arr[0]。比较(obj2Arr[0]);
}
});

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

试试下面的循环

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(int i=0;i
或使用特定字段对数据进行排序

在使用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());
  }
Arrays.sort(文件,新比较器()
{
公共整数比较(对象o1、对象o2){
如果(((文件)o1.lastModified()>((文件)o2.lastModified()){
返回-1;
}else if(((文件)o1.lastModified()<((文件)o2.lastModified()){
返回+1;
}否则{
返回0;
}
}
});
对于(int i=0;i
Read您需要使用文件属性(如lastmodified日期和时间)来显示imagestry,使用collections.sort根据文件名对其进行排序method@Rat-a-tat-a-tatratotouille你能给我指路吗?@Moon试试答案..兄弟不明白这个:字符串obj1,字符串obj2,你可以看到更多的代码,可能对教我有帮助,检查此链接:在构造函数内的适配器类中,编写此代码以对其进行排序,但我不确定是否隐藏了进度条以及所有内容。因此,我是否需要将给定的代码放置在适配器类的构造函数中,对吗?要对记录进行排序,请在顶部使用旧文件名,在底部使用最新文件名!为什么?有什么办法可以解决这个问题吗?我在给我的图片命名时也使用了counter,比如:“-”+getNextNumber()+//counter。。。。。。所以我们可以用这个计数器在顶部显示最新的文件吗?还是一个小问题,比如我在一个列表中有5个图像,我删除了3个数字图像,然后捕获了另一个图像,现在回到列表,得到6个捕获的图像,而不是3。。为什么?鉴于第6张图片必须显示在顶部,我还发布了代码,我正在使用该代码删除listbrother中的图片,无论您何时来,请尝试解决此问题:
 for (int i = 0; i < files.length; i++)
 {
      file = files[i];
      Log.d("Count",file.getPath());
      it.add (file.getPath());
  }
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());
  }