Home Android FilePicker GridView Images

Android FilePicker GridView Images

Android FilePicker GridView Images




Quick Intro

Look the aim is simple.To load images from our File System and into our GridView.Maybe we can do that via a loop but see we want control.We want to select images we load,not just loop through and load all images,though we have such a tutorial as well.So we shall be using a FilePicker,a material filepicker third party library to help us choose images with a nice material interface.Moreover we can define the total number of images users can choose at a go.

We then display these images in a GridView.

So the first thing is to add teh dependency in our build.gradle app level,as in :

compile 'com.droidninja:filepicker:1.0.0'

 

We shall have something like this,simply overriding our onActivityResult() method in our MainActivity.We have a model class we called spacecraft,we simply then assign it properties depending on what we receive from the files we are loading.  :

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode)
        {
            case FilePickerConst.REQUEST_CODE:

                if(resultCode==RESULT_OK && data!=null)
                {
                    filePaths = data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_PHOTOS);
                    Spacecraft s;
                    ArrayList<Spacecraft> spacecrafts=new ArrayList<>();

                    try
                    {
                        for (String path:filePaths) {
                            s=new Spacecraft();
                            s.setName(path.substring(path.lastIndexOf("/")+1));

                            s.setUri(Uri.fromFile(new File(path)));
                            spacecrafts.add(s);
                        }

                        gv.setAdapter(new CustomAdapter(this,spacecrafts));
                        Toast.makeText(MainActivity.this, "Total = "+String.valueOf(spacecrafts.size()), Toast.LENGTH_SHORT).show();
                    }catch (Exception e)
                    {
                        e.printStackTrace();
                    }



                }

        }
    
}

 

Of course wevrything is in the source code reference above,so please download it and import into your android studio.Its well commented.

 

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    seventeen + 6 =

    Leave a Comment

    nine − 4 =