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.