3

//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);


    }
}
Likith Ts
  • 296
  • 1
  • 7
  • 18
  • Why don't you sort the list before you use it to populate the grid view? – iRuth Jan 27 '15 at 01:45
  • Can u provide me an example for this ? – Likith Ts Jan 27 '15 at 02:18
  • Could you post the full grid view adapter code that you have written? – iRuth Jan 27 '15 at 02:32
  • Did you sort the `itemList` before instantiating the `ImageAdapter` in your activity? – iRuth Jan 27 '15 at 04:10
  • Check out my activity class . Let me know which method I have to use for sorting. – Likith Ts Jan 27 '15 at 06:49
  • Need one more help, every time when I scroll the grid view . This method is executed. Hence making the scroll not so smooth. public Bitmap decodeFile(String path, int reqWidth, int reqHeight) { ..................} . Help me running the above method in background. – Likith Ts Jan 27 '15 at 08:09

2 Answers2

2

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.

iRuth
  • 2,737
  • 3
  • 27
  • 32
  • How to sort in descending order ? As of now new images are saving at the end instead of the beginning of the grid view, – Likith Ts Jan 27 '15 at 15:15
  • After changing the code, still the result is same. I need exactly like gallery. Whenever a new photo is taken from camera. It appears at the beginning. Any further changes ? – Likith Ts Jan 28 '15 at 01:59
  • Thank You . Its working now . Any suggestion on make scroll smooth, for the above code. – Likith Ts Jan 28 '15 at 02:21
  • I'm glad your problem is solved. Are you still decoding the bitmap inside the `getView` method? – iRuth Jan 28 '15 at 02:23
  • You should decode them before you populate the gridview. Refer to the link in my answer. – iRuth Jan 28 '15 at 02:46
  • Just now found a bug. My grid view is perfect now. Inside grid view when I click on any item. It was opening a FullScreenImage . My fullScreenImage class accept a path of the file which is opened. The path which i'm receiving perfect. But the image displayed in imageview is always the first photo which I have taken. Not the one which I have clicked. But when I share image through gmail , image is perfectly . Because the path is right. But only when displaying in fullscreen its showing me the first photo which was taken for all gridview items, – Likith Ts Jan 28 '15 at 02:47
  • Please post it as a new question. – iRuth Jan 28 '15 at 03:00
  • Please have a look on this http://stackoverflow.com/questions/28192797/open-full-screen-image-when-clicked-on-grid-view-android – Likith Ts Jan 28 '15 at 12:55
  • @iRuth according to the link you post and you advice, we should decode the image before and in the link they put them in ArrayList, I guess the ArrayList is a list of custom class which has Bitmap and title right, so what if I want that ArrayList instance accessible in everywhere? Should I make it static? Because you said don't decode bitmap twice – truongnm May 01 '16 at 11:29
  • @truongnm, what do you mean by "everywhere"? – iRuth May 01 '16 at 13:14
  • 1
    @iRuth I mean in multiple activities. Ahh, never mind, I didn't read the hold tutorial, now I get it :v – truongnm May 01 '16 at 14:37
0

For making your scroll smooth you have to display the images with Picasso library, I had the same issue !

CDrosos
  • 2,418
  • 4
  • 26
  • 49