Android-按日期对图像排序

时间:2018-12-09 00:08:10

标签: android sorting android-recyclerview

我正在Android上尝试简单的操作。 我正在从外部存储中获取图像并将其保存在数组中。 我想做的是显示按日期排序的所有图像。 我正在使用Reyclerview显示图像。 知道如何按日期对它们进行排序吗?

我的代码示例

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    rvImages = (RecyclerView)findViewById(R.id.rvPictures);

                imageLists = getFilePaths();

                    setAdapter(imageLists);
      }


     public ArrayList<String> getFilePaths()
{


    Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    String[] projection = {MediaStore.Images.ImageColumns.DATA};
    Cursor c = null;
    SortedSet<String> dirList = new TreeSet<String>();
    ArrayList<String> resultIAV = new ArrayList<String>();

    String[] directories = null;
    if (u != null)
    {
        c = managedQuery(u, projection, null, null, null);
    }

    if ((c != null) && (c.moveToFirst()))
    {
        do
        {
            String tempDir = c.getString(0);
            tempDir = tempDir.substring(0, tempDir.lastIndexOf("/"));
            try{
                dirList.add(tempDir);
            }
            catch(Exception e)
            {
                Log.d("MainActivity",e.getMessage().toString());
            }
        }
        while (c.moveToNext());
        directories = new String[dirList.size()];
        dirList.toArray(directories);

    }

    for(int i=0;i<dirList.size();i++)
    {
        File imageDir = new File(directories[i]);
        File[] imageList = imageDir.listFiles();
        if(imageList == null)
            continue;
        for (File imagePath : imageList) {
            try {

                if(imagePath.isDirectory())
                {
                    imageList = imagePath.listFiles();

                }
                if ( imagePath.getName().contains(".jpg")|| imagePath.getName().contains(".JPG")
                        || imagePath.getName().contains(".jpeg")|| imagePath.getName().contains(".JPEG")
                        || imagePath.getName().contains(".png") || imagePath.getName().contains(".PNG") )
                {
                    String path= imagePath.getAbsolutePath();

                    resultIAV.add(path);
                }
            }
            //  }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    return resultIAV;

}


public class ImageViewAdapter extends RecyclerView.Adapter<ImageViewHolder>{
    private LayoutInflater inflater;
    private Context context;
    private ArrayList<String> data;

    public ImageViewAdapter(Context context, ArrayList<String> data){
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.data = data;
    }

    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = inflater.inflate(R.layout.image_item,parent,false);
        return new ImageViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
            String imgSrc = data.get(position);
            Uri imageUri = Uri.parse(imgSrc);
            Log.d("MainActivity:ImagUri: ",imageUri.toString());
           holder.imgView.setImageURI(imageUri);

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

}


public class ImageViewHolder extends RecyclerView.ViewHolder{
    private ImageView imgView;

    public ImageViewHolder(View itemView){
        super(itemView);
        imgView = (ImageView)itemView.findViewById(R.id.imgView);
    }
}

解决方案可能在光标处?

0 个答案:

没有答案