Android 从sqlite数据库检索图像引用并在gridview中显示它们
我之前发布了关于在应用程序的文件系统中存储和检索图像引用的文章。我对如何在Sq-lite数据库中存储这些引用有了一些见解,并决定将图像的“名称”存储在指定的列中 目前,我使用的代码允许我通过直接从目录访问文件夹,在网格视图中显示所有捕获的图像: MainActivity.javaAndroid 从sqlite数据库检索图像引用并在gridview中显示它们,android,sqlite,gridview,android-sdcard,Android,Sqlite,Gridview,Android Sdcard,我之前发布了关于在应用程序的文件系统中存储和检索图像引用的文章。我对如何在Sq-lite数据库中存储这些引用有了一些见解,并决定将图像的“名称”存储在指定的列中 目前,我使用的代码允许我通过直接从目录访问文件夹,在网格视图中显示所有捕获的图像: MainActivity.java public class MainActivity extends Activity{ //Initializing Variables: private String[] FilePathStr
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);
}
});
}
}
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;
}
}
}
仅在目录中显示图像本身就可以很好地工作,但是我希望能够根据存储在数据库中的文件名检索和显示这些图像
换句话说,我希望能够仅查看数据库中存储有该引用的图像
欢迎提供任何建议、示例代码和反馈。检查列表中的文件名是否存在于getview()的目录中。如果存在,请使用imageLoader.DisplayImage(文件路径[位置],图像)加载;嘿@playmaker420,谢谢你的回复。列表中的文件名已存在于目录中,是否要检查数据库中是否存在该文件名。我该如何查询类似的内容?我的意思是从数据库中选择所有图像url并将其保存在列表中。然后遍历列表并检查文件夹中列表中的每个可用项。如果是,则使用图像加载器加载图像,否则加载“图像不可用”图像。检查是否有帮助,我不确定你想达到什么目的这正是我想要达到的。谢谢你的帮助!