从sqlite数据库中检索图像引用并将其显示在gridview中

时间:2013-11-29 07:38:08

标签: android sqlite gridview android-sdcard

我之前发布了关于在我的应用程序的文件系统HERE中存储和检索图像引用的文章。我深入了解了如何在Sq-lite数据库中存储这些引用,并决定将图像的“名称”存储在指定的列中。

目前我正在使用的代码允许我通过直接访问目录中的文件夹来在网格视图中显示所有捕获的图像:

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; 
        }
    }              

                                                                    }

这仅仅可以在目录中显示图像,但我希望能够根据数据库中存储的文件名检索和显示这些图像。

换句话说,我希望能够仅查看具有存储在数据库中的引用的图像。

欢迎任何建议,示例代码和反馈。

0 个答案:

没有答案