I am a beginner developer, and I am currently working on an application in which part of the functions allows users to capture an image, store it in the application file system and store its link in the SQLite database column. Then the user will be able to view these images in a gridview based on any criteria with which he is associated in the database (for example, only displaying images of a certain color).
First, I actually “grabbed” the captured image file name and saved it to the database using this code:
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);
File dir = new File("sdcard/item_images");
try
{
if(dir.mkdir())
{
System.out.println("Directory created");
}
else
{
System.out.println("Directory is not created");
}
}
catch(Exception e)
{
e.printStackTrace();
}
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 )
{
case 0:
break;
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;
}
}
However, I realized that the stored file name is a regular string in the application context and does not actually indicate any image stored in the file system.
What I would like to know:
, , .