简体   繁体   English

如何对资产文件夹中的文件排序?

[英]How to sort files in assets folder?

Here are faced with the problem of sorting files. 这里面临着文件排序的问题。 Choose the files from the folder Asset. 从文件夹Asset中选择文件。 How to sort files in ascending? 如何对文件升序排序? Here is my code: 这是我的代码:

//fillGrid
private void fillGridAdapter(int cat) {
   ASSETS_IMAGE_DIR = imagePath[cat];
   addImages(getImages(imagePath[cat]));
}
//Adds the files
private void addImages(String[] temp){
   imBitmap = new Bitmap[temp.length];
   if(temp != null) {
   for(int i = 0; i < temp.length; i++){
       try {
    imBitmap[i] = getBitmapFromAsset(imagePath[g.getImageCat()]+"/"+temp[i]);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
      }
    }
}
private String[] getImages(String f){
        try {
        AssetManager assetManager = getResources().getAssets();
        String[] temp = assetManager.list(f);

        Arrays.sort(temp);
        return temp;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
}

After assetManager.list(f) String[] temp - (1.jpg, 10.jpg, 12.jpg ... 9.jpg). 在assetManager.list(f)之后的String [] temp-(1.jpg,10.jpg,12.jpg ... 9.jpg)。 After Arrays.sort(temp) - (1.jpg, 10.jpg, 12.jpg ... 9.jpg). After Arrays.sort(temp)-(1.jpg,10.jpg,12.jpg ... 9.jpg)。 And I need to - 1.jpg, 2.jpg, 3.jpg... n.jpg. 我需要-1.jpg,2.jpg,3.jpg ... n.jpg。

It sounds like you want to sort the files into numeric order ... not lexical order. 听起来您想将文件排序为数字顺序,而不是词汇顺序。

To achieve this, you will need to split the pathnames into a numeric and non-numeric segments. 为此,您需要将路径名分为数字和非数字段。 For the numeric segment(s) you need to parse the segment as an integer and sort based on the integer values. 对于数字段,您需要将段解析为整数,然后根据整数值进行排序。

It looks like your files are of the form <number>.<suffix> , so the splitting should be simple. 看来您的文件格式为<number>.<suffix> ,所以拆分应该很简单。

This logic needs to be implemented in the compare method of a Comparator which you provides a parameter to sort . 此逻辑需要在Comparatorcompare方法中实现,您可以提供一个要sort的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM