Saturday, January 2, 2016

Android Application - Load Images from App Data Directory and Display in Gallery

Hello,

Recently in one of my project, we have list of images in one of the application directory. The requirement was to read those images and display like a gallery. In this blog I am going to explain how to do this.

First of all add following file in your layout xmls and name it activity_image_list.xml. Following is the code.

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/imageGridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:columnWidth="120dp"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:gravity="center"
    android:stretchMode="columnWidth"  >

</GridView>

This is the Grid View which will show the images like a gallery. Now add an activity and set this xml as contentView in onCreate method.

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_list);


Now for grid we need to have adapter so I created following class and add it to activity.

public class ImageAdapter extends BaseAdapter{
        private Context mContext;
        public int getCount() {
             return mThumbIds.length;
        }                               
        public Object getItem(int position) {
             return mThumbIds[position];
        }                               
        public long getItemId(int position) {
             return 0;
        }                               
        public ImageAdapter(Context c) {
             mContext = c;
        }    
        
        public void setFiles(File[] savedSnapshots){
        this.mThumbIds = savedSnapshots;
        }
                              
        public View getView(int position, View convertView, ViewGroup parent) {
             ImageView imageView;
             if (convertView == null){  
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
             } 
             else{
                 imageView = (ImageView) convertView;
             }
             try{
             Bitmap thumbnail = BitmapFactory.decodeFile(mThumbIds[position].getAbsolutePath());
                 imageView.setImageBitmap(thumbnail);
             }catch(Exception e){
             
             }
             return imageView;
        }
                             
        private File[] mThumbIds = {};        
    }

Now we will load list of images from specific directory and display it in grid,

GridView gridview = (GridView) findViewById(R.id.imageGridview);
ImageAdapter adapter = new ImageAdapter(this);
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("snapshots", Context.MODE_PRIVATE);
snapshots = directory.listFiles();
adapter.setFiles(snapshots);
gridview.setAdapter(adapter);

As you can see above we are reading images from snapshots directory and create an adapter and set list of files in it. 

And inside getView method of adapter we are creating bitmap and assigned it to image view. This way images will be displayed in grids..

Hope this helps you.

No comments:

Post a Comment