Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort Image according to DATE and TIME inside gridview android

//The Path where my images are saving

/storage/emulated/0/Pictures/CameraExample/JPEG_20150107_152640.jpeg

//This is one of the file path.

//I need to sort images like last taken photo as the first one to display //inside the grid view as of now images are displayed, recent photo at the //last.

Below is my code

 public class ImageAdapter extends BaseAdapter{


        private Context context;
        private int imageWidth;
        private Activity activity;
        ArrayList<String> itemList = new ArrayList<String>();
        Bitmap bitmap;

        public ImageAdapter(Activity activity,ArrayList<String> itemList,int imageWidth) {
            this.activity = activity;
            this.itemList = itemList;
            this.imageWidth = imageWidth;

        }

        @Override
        public int getCount() {

            return this.itemList.size();
        }

        void add(String path) {
            itemList.add(path);

        }

        @Override
        public Object getItem(int position) {
            return this.itemList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;

            if (convertView == null) {

                imageView = new ImageView(activity);


            }
        else {
                imageView = (ImageView) convertView;

            }

            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

            imageView.setLayoutParams(new GridView.LayoutParams(imageWidth ,imageWidth ));


            bitmap = decodeFile(itemList.get(position), imageWidth, imageWidth);

             imageView.setImageBitmap(bitmap);

            imageView.setOnClickListener( new OnImageClickListener(position));


            return imageView;
        }


        class OnImageClickListener implements View.OnClickListener {

            int position;

            public OnImageClickListener(int position){
                this.position = position;
            }

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, ImageDisplayActivity.class);               

                intent.putExtra("position", position);    

                Log.d("ImageAdapter","Intent.putExtra ");
                activity.startActivity(intent);

            }
        }

        public Bitmap decodeFile(String path, int reqWidth, int reqHeight) {
            try {
                File f = new File(path);     
                BitmapFactory.Options o = new BitmapFactory.Options();

                o.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(new FileInputStream(f), null, o);
                final int REQUIRED_WIDTH = reqWidth;
                final int REQUIRED_HEIGHT = reqHeight;
                int scale = 1;
                while (o.outWidth / scale / 2 >= REQUIRED_WIDTH && o.outHeight / scale / 2 >= REQUIRED_HEIGHT)
                    scale *= 2;

                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize = scale;
                return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

// And Finally when I scroll my grid view . Scrolling is not so smooth. Please help me in solving two problems.

// My GridView Activity Class

public class GridViewActivity extends Activity {

    private ImageAdapter imageAdapter;
    private HelperUtils utils;
    private ArrayList<String> itemList = new ArrayList<String>();
    private GridView gridView;
    private int columnWidth;
    private Activity activity;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gridview);

        gridView = (GridView) findViewById(R.id.gridView);

        utils = new HelperUtils(this);

        InitilizeGridLayout();

        itemList = utils.getFilePaths();

        imageAdapter = new ImageAdapter(this, itemList, columnWidth);

        gridView.setAdapter(imageAdapter);

    }

    private void InitilizeGridLayout() {

        Resources r = getResources();
        float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, HelperAppConstant.GRID_PADDING, r.getDisplayMetrics());
        columnWidth = (int) ((utils.getScreenWidth() - ((HelperAppConstant.NUM_OF_COLUMNS + 1) * padding)) / HelperAppConstant.NUM_OF_COLUMNS);

        gridView.setNumColumns(HelperAppConstant.NUM_OF_COLUMNS);
        gridView.setColumnWidth(columnWidth);
        gridView.setStretchMode(GridView.NO_STRETCH);
        gridView.setPadding((int) padding, (int) padding, (int) padding, (int) padding);
        gridView.setHorizontalSpacing((int) padding);
        gridView.setVerticalSpacing((int) padding);


    }
}
like image 767
Likith Ts Avatar asked Nov 19 '25 13:11

Likith Ts


1 Answers

Remove the Collections.sort(itemList); from your getView method.

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;

        if (convertView == null) {
            Log.d("ImageAdapter","Inside the if condition");
            imageView = new ImageView(activity);

        }
    else {
            imageView = (ImageView) convertView;
          }

        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(imageWidth ,imageWidth ));

       // bitmap = decodeFile(itemList.get(position), imageWidth, imageWidth);

        bitmap = decodeFile(getItem(position), imageWidth, imageWidth);

        imageView.setImageBitmap(bitmap);

        return imageView;
    }

Sort the list just before you populate your grid view in your onCreate method.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gridview);

        gridView = (GridView) findViewById(R.id.gridView);

        utils = new HelperUtils(this);

        InitilizeGridLayout();

        itemList = utils.getFilePaths();

        Collections.sort(itemList);

        imageAdapter = new ImageAdapter(this, itemList, columnWidth);

        gridView.setAdapter(imageAdapter);

    }

You could follow the example in this link by creating a list of items that contains the image file path and the image bitmap. This way, you wouldn't have to decode the bitmap everytime getView is called. You could just decode it once before you populate the gridview.

Since the images are sorted in ascending order, you can replace you getItem method with the one below:

    @Override
    public Object getItem(int position) {
        return this.itemList.get(itemList.size() - 1 - position);
    }

I updated the getView method. You should use getItem(position) instead of itemList.get(position) to decode the bitmap.

like image 132
iRuth Avatar answered Nov 22 '25 02:11

iRuth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!