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{
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);
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
Toast.makeText(this, "Error! No SDCARD Found!", Toast.LENGTH_LONG).show();
}
else
{
file = new File(Environment.getExternalStorageDirectory() + File.separator + "item_images");
file.mkdirs();
}
if (file.isDirectory())
{
listFile = file.listFiles();
FilePathStrings = new String[listFile.length];
FileNameStrings = new String[listFile.length];
for (int i = 0; i < listFile.length; i++)
{
FilePathStrings[i] = listFile[i].getAbsolutePath();
FileNameStrings[i] = listFile[i].getName();
}
}
grid = (GridView) findViewById(R.id.gridview);
adapter = new GridViewAdapter(this, FilePathStrings, FileNameStrings);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent i = new Intent(getApplicationContext(), ViewImage.class);
i.putExtra("filepath", FilePathStrings);
i.putExtra("filename", FileNameStrings);
i.putExtra("position", position);
startActivity(i);
}
});
}
}
GridViewAdapter.java
public class GridViewAdapter extends BaseAdapter {
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());
}
public int getCount()
{
return filepath.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
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.
, , .
, , .