0

I previously posted about storing and retrieving references to images in my application's file system HERE. I got some insight into how I should store those references in my Sq-lite database and decided to store the "name" of the image in a designated column.

Currently the code I am using allows me to display all the captured images in a grid-view, by directly accessing the folder from the directory:

MainActivity.java

 public class MainActivity extends Activity{

    //Initializing Variables:
     private String[] FilePathStrings;
     private String[] FileNameStrings;
     private File[] listFile;
     GridView grid;
     GridViewAdapter adapter;
     File file;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.closetui);

        // Check device for SD Card:
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) 
        {
            Toast.makeText(this, "Error! No SDCARD Found!", Toast.LENGTH_LONG).show();
        } 
        else 
        {
            // Locate the item_images folder in your SD Card:
            file = new File(Environment.getExternalStorageDirectory() + File.separator + "item_images");

            // Create a new folder if no folder named item_images exist:
            file.mkdirs();
        }

        if (file.isDirectory()) 
        {
            //Creating a list of files from the "item_images" folder:
            listFile = file.listFiles();

            // Create a String array for FilePathStrings:
            FilePathStrings = new String[listFile.length];

            // Create a String array for FileNameStrings:
            FileNameStrings = new String[listFile.length];

            for (int i = 0; i < listFile.length; i++) 
            {
                // Get the path of the image file:
                FilePathStrings[i] = listFile[i].getAbsolutePath();

                // Get the name image file:
                FileNameStrings[i] = listFile[i].getName();
            }
        }

        // Locate the GridView in activityMain.xml:
        grid = (GridView) findViewById(R.id.gridview);

        // Pass String arrays to GridViewAdapter Class:
        adapter = new GridViewAdapter(this, FilePathStrings, FileNameStrings);

        // Set the GridViewAdapter to the GridView:
        grid.setAdapter(adapter); 

        // Capture gridview item click:
        grid.setOnItemClickListener(new OnItemClickListener() 
        {
            @Override                        //<?> - Generic type
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                Intent i = new Intent(getApplicationContext(), ViewImage.class);

                // Pass String arrays FilePathStrings:
                i.putExtra("filepath", FilePathStrings);

                // Pass String arrays FileNameStrings:
                i.putExtra("filename", FileNameStrings);

                // Pass click position:
                i.putExtra("position", position);
                startActivity(i);
            }
        });
    }
}

GridViewAdapter.java

public class GridViewAdapter extends BaseAdapter {

    // Variable Declarations:
    private Activity activity;
    private String[] filepath;
    private String[] filename;
    private static LayoutInflater inflater = null;
    public ImageLoader imageLoader;

    public GridViewAdapter(Activity a, String[] fpath, String[] fname) 
    {
        activity = a;
        filepath = fpath;
        filename = fname;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        imageLoader = new ImageLoader(activity.getApplicationContext());
    }

    //Returns the number of images:
    public int getCount() 
    {
        return filepath.length;
    }

    //Returns the id of an item:
    public Object getItem(int position) 
    {
        return position;
    }

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

    //Returns an image view:
    public View getView(int position, View convertView, ViewGroup parent) 
    {       
        View vi = convertView;

        if (convertView == null) vi = inflater.inflate(R.layout.gridview_item, null);
        {

            ImageView image = (ImageView) vi.findViewById(R.id.image);

            imageLoader.DisplayImage(filepath[position], image);

            return vi; 
        }
    }              

                                                                    }

This works fine on its own for just displaying images in the directory, however I want to be able to retrieve and display these images based on their stored file name in the database.

In other words, I want to be able to only see images that have that reference stored in the database.

Any suggestions, sample code, feedback is welcomed.

Community
  • 1
  • 1
ImNewHere
  • 45
  • 1
  • 7
  • Check whether the filename from the list exists in the the dirctory in side your getview(). If exists load using imageLoader.DisplayImage(filepath[position], image); – playmaker420 Nov 29 '13 at 12:38
  • Hey @playmaker420, thanks for your reply. The filename from the list does exist in the directory already, did you mean to check if it exists in the _database?_ . How would I go about querying something like that? – ImNewHere Nov 29 '13 at 13:57
  • i mean select all image url from your database and keep it in a list.Then iterate through the list and check the each item in the list available in the folder.If yes load the image using your image loader else load a "image not avaible" image.Check whether it helps you ,Im not sure what are you trying to achieve – playmaker420 Nov 30 '13 at 05:02
  • That's exactly what I want to achieve. Thanks for your help! – ImNewHere Dec 02 '13 at 16:44

0 Answers0