Getting links to images from sqlite database and displaying them in a grid

I previously posted information about storing and retrieving image links in my application file system HERE . I got some idea of ​​how I should store these links in my Sq-lite database and decided to save the "name" of the image in the designated column.

Currently, the code I'm using allows me to display all captured images in a grid by directly accessing a folder from a 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 great for easily displaying images in a directory, however I want to be able to extract and display these images based on their stored file name in the database.

, , .

, , .

0

Source: https://habr.com/ru/post/1527660/


All Articles