Layout/imageswitch. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<ImageSwitcher
Android: id = "@ + id/arc_hf_img_switcher"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: layout_alignParentLeft = "true"
Android: layout_alignParentTop = "true"/>
<LinearLayout
Android: id = "@ + id/linearLayout1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_alignParentBottom = "true"
Android: layout_alignParentLeft = "true"
Android: gravity = "center">
<Gallery
Android: id = "@ + id/arc_hf_img_gallery"
Android: layout_width = "fill_parent"
Android: layout_height = "60dp"
Android: layout_weight = "1"
Android: background = "#55000000"
Android: gravity = "center_vertical"
Android: spacing = "16dp"/>
<Button
Android: id = "@ + id/arc_hf_img_btnDel"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginRight = "30dp"
Android: text = "@ string/arc_hf_img_btnDel"/>
<Button
Android: id = "@ + id/arc_hf_img_btnBack"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginRight = "30dp"
Android: text = "@ string/arc_hf_img_btnBack"/>
</LinearLayout>
</RelativeLayout>
ImgSwitch. java
Public class ImgSwitch extends Activity implements OnItemSelectedListener,
ViewFactory {
Private final String DATABASE_PATH = Environment
. GetExternalStorageDirectory (). getAbsolutePath () + "/hfdatabase ";
Private final String DATABASE_FILENAME = "hf_database.db ";
Private final String TABLE_NAME = "HF_IMG_THEME ";
Private final String strImgPath = Environment. getExternalStorageDirectory ()
. ToString () + "/dlion/"; // folder for storing photos
Private SQLiteDatabase db;
Private ImageSwitcher is;
Private Gallery gallery;
Private Bitmap [] bitmaps;
Private int [] _ ids;
Private String [] imgNames;
Private int parentId;
Public int gPosition = 0;
Private int isPosition = 0;
Private Button btnDel;
Private Button btnBack;
// Listen to the Return key to add and disable the database function.
Public void onBackPressed (){
Super. onBackPressed ();
Db. close ();
Finish ();
}
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. imageswitch );
Db = openDatabase ();
ParentId = this. getIntent (). getIntExtra ("parentId", 0 );
Gallery = (Gallery) findViewById (R. id. arc_hf_img_gallery );
Is = (ImageSwitcher) findViewById (R. id. arc_hf_img_switcher );
Is. setFactory (this );
Is. setInAnimation (AnimationUtils. loadAnimation (this,
Android. R. anim. fade_in ));
Is. setOutAnimation (AnimationUtils. loadAnimation (this,
Android. R. anim. fade_out ));
BtnDel = (Button) findViewById (R. id. arc_hf_img_btnDel );
BtnBack = (Button) findViewById (R. id. arc_hf_img_btnBack );
BtnDel. setOnClickListener (new btnListener ());
BtnBack. setOnClickListener (new btnListener ());
InitImgs ();
}
// Button listening
Class btnListener implements OnClickListener {
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Switch (v. getId ()){
Case R. id. arc_hf_img_btnBack:
Db. close ();
Finish ();
Break;
Case R. id. arc_hf_img_btnDel:
Db. delete (TABLE_NAME, "_ id =" + _ ids [gPosition], null );
File file = new File (imgNames [gPosition]);
File. delete ();
Toast. makeText (ImgSwitch. this, imgNames [gPosition] + "deleted successfully ",
Toast. LENGTH_SHORT). show ();
InitImgs ();
Break;
Default:
Break;
}
}
}
// Initialize the image View
Private void initImgs (){
Cursor cursorImgName = db. query (TABLE_NAME, null, "parentId ="
+ ParentId, null );
Bitmaps = new Bitmap [cursorImgName. getCount ()];
ImgNames = new String [cursorImgName. getCount ()];
_ Ids = new int [cursorImgName. getCount ()];
// Optimized memory
BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inSampleSize = 20;
For (int I = 0; I <cursorImgName. getCount (); I ++ ){
CursorImgName. moveToNext ();
// Save the image group name, ID, and complete path
ImgNames [I] = strImgPath
+ CursorImgName. getString (cursorImgName
. GetColumnIndex ("imgName "));
_ Ids [I] = cursorImgName. getInt (cursorImgName. getColumnIndex ("_ id "));
Bitmaps [I] = BitmapFactory. decodeFile (imgNames [I], options );
}
Gallery. setAdapter (new ImageAdapter (this ));
Gallery. setOnItemSelectedListener (this );
If (gPosition = cursorImgName. getCount ()
& CursorImgName. getCount ()> = 1 ){
Is. setImageURI (Uri. parse (imgNames [isPosition-1]);
} Else if (cursorImgName. getCount () = 0) {// when no image exists, cancel the delete key listening.
BtnDel. setEnabled (false );
}
}
@ Override
Public View makeView (){
// TODO Auto-generated method stub
ImageView I = new ImageView (this );
I. setBackgroundColor (0xFF000000 );
I. setScaleType (ImageView. ScaleType. FIT_CENTER );
I. setLayoutParams (new ImageSwitcher. LayoutParams (
LayoutParams. FILL_PARENT, LayoutParams. FILL_PARENT ));
Return I;
}
// Override the baseadapter class
Public class ImageAdapter extends BaseAdapter {
Private Context mContext;
Public ImageAdapter (Context c ){
MContext = c;
}
@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return bitmaps. length;
}
@ Override
Public Object getItem (int position ){
// TODO Auto-generated method stub
Return position;
}
@ Override
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return position;
}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
// TODO Auto-generated method stub
GPosition = position;
ImageView I = new ImageView (mContext );
I. setImageBitmap (bitmaps [position]);
I. setAdjustViewBounds (true );
I. setLayoutParams (new Gallery. LayoutParams (
LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT ));
Return I;
}
}
@ Override
Public void onItemSelected (AdapterView <?> Arg0, View arg1, int arg2,
Long arg3 ){
// TODO Auto-generated method stub
IsPosition = arg2;
Is. setImageURI (Uri. parse (imgNames [arg2]);
}
@ Override
Public void onNothingSelected (AdapterView <?> Arg0 ){
// TODO Auto-generated method stub
}
// Initialize the database
Private SQLiteDatabase openDatabase (){
Try {
String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
File dir = new File (DATABASE_PATH );
If (! Dir. exists ())
Dir. mkdir ();
If (! (New File (databaseFilename). exists ()){
InputStream is = getResources (). openRawResource (
R. raw. hf_database );
FileOutputStream fos = new FileOutputStream (databaseFilename );
Byte [] buffer = new byte [8192];
Int count = 0;
// Start copying the hf_database.db File
While (count = is. read (buffer)> 0 ){
Fos. write (buffer, 0, count );
}
}
Db = SQLiteDatabase. openOrCreateDatabase (databaseFilename, null );
Return db;
} Catch (Exception e ){
E. printStackTrace ();
}
Return null;
}
}
From oldfeel