【发布时间】:2013-11-28 02:24:27
【问题描述】:
我是一名新手开发人员,目前正在开发一个应用程序,其中部分功能允许用户捕获图像,将其存储在应用程序的文件系统中,并将其引用存储在 SQLite 数据库的列中。然后,用户将能够根据与数据库中关联的任何标准(例如,仅显示某种颜色的图像)在网格视图中查看这些图像。
一开始我实际上“抓取”了捕获的图像的文件名并将其存储在数据库中,使用以下代码:
//Initializing Variables:
protected ImageButton _button;
protected ImageView _image;
protected TextView _field;
protected String _path;
protected String name;
protected boolean _taken;
protected static final String PHOTO_TAKEN = "photo_taken";
@Override
protected void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onCreate(savedInstanceState);
setContentView(R.layout.newitemui);
//Creating the "item_images" directory and File:
File dir = new File("sdcard/item_images");
try
{
//Create new directory:
if(dir.mkdir())
{
System.out.println("Directory created");
}
else
{
System.out.println("Directory is not created");
}
}
catch(Exception e)
{
e.printStackTrace();
}
//Setting the current time stamp for image name:
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
_image = ( ImageView ) findViewById( R.id.image );
_field = ( TextView ) findViewById( R.id.field );
_button = ( ImageButton ) findViewById( R.id.button );
_button.setOnClickListener( new ButtonClickHandler() );
name = "IMG_" + timeStamp + ".png";
_path = Environment.getExternalStorageDirectory() + File.separator + "/item_images" + "/" + name;
Toast toast = Toast.makeText(NewItemUI.this,"Touch Camera to Capture Image", Toast.LENGTH_LONG);
toast.show();
toast.setGravity(Gravity.DISPLAY_CLIP_HORIZONTAL|Gravity.BOTTOM, 0, 200);
}
public class ButtonClickHandler implements View.OnClickListener
{
public void onClick( View view ){
startCameraActivity();
}
}
protected void startCameraActivity()
{
File file = new File( _path );
Uri outputFileUri = Uri.fromFile( file );
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
startActivityForResult( intent, 0 );
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch( resultCode )
{
//If user did not take photo, return to the original screen with no result:
case 0:
break;
//If user took a photo, insert the image name into database and return to the original screen with the captured image displayed:
case -1:
AppDatabase appdb = new AppDatabase(this);
SQLiteDatabase sql = appdb.getWritableDatabase();
sql.execSQL("INSERT INTO tblClothingItem (imagePath) VALUES ('"+name+"');");
sql.close();
appdb.close();
onPhotoTaken();
break;
}
}
但是我意识到存储的文件名只是应用程序上下文中的一个普通字符串,实际上并不指向存储在文件系统中的任何一个图像。
我想知道的是:
- 如何在我的 SQLite 数据库中存储图像的 Uri 被捕获
- 我将如何根据存储的图像检索您的图像 在网格视图中显示的 Uri。
欢迎任何建议、示例代码、反馈。
【问题讨论】:
-
正如你所说,URI实际上只是一个特定格式的文本。您可以将其存储为
String(或 SQLite 中的TEXT)。要从String获取相同的URI,可以使用Uri.parse() -
@AndrewT。感谢您的快速回复和建议。
-
@AndrewT。如果它只是一个文本,我可以使用我已经存储在数据库中的文件名并使用它来检索 gridview 中的图像吗?
-
是的。但是,这取决于您如何为
GridView使用/创建适配器。这些是我想到的一些可能的情况:1)只存储没有完整路径的文件名(假设所有图像都有一个共同的根目录),2)存储完整路径+文件名,3)存储URI。所有这些情况都是可能的,真正的问题是如何使用适配器中的数据......我希望这能给你带来洞察力:) -
@AndrewT。这确实是我要研究的问题和事情。我正在捕获的图像存储在同一个文件夹中,因此我相信您的第一个建议(这是我的代码当前执行的)应该可以正常工作。我目前拥有的 gridview 通过直接访问目录来显示文件夹中存储的所有图像,但我希望能够根据用户选择的标准显示/排序这些图像。在数据库中存储对图像的引用允许我附加一个唯一标识符以促进该功能。非常感谢您的反馈!