PNG image -> SQLite DB -> imageview

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Re: PNG image -> SQLite DB -> imageview

Postby rockyknight007 » Mon Oct 04, 2010 4:23 am

Hello Experts

I am a new baby for Android developments.
Please give me information for -
(1) Can communicate "Andriod and WCF (.Net)" ?
(2) If can communicate , please develop a very small app for me. (Hello World or Small Calculator (Addition)).

Best Regards
Kyaw
rockyknight007
Freshman
Freshman
 
Posts: 4
Joined: Mon Oct 04, 2010 3:42 am

Top

Re: PNG image -> SQLite DB -> imageview

Postby hero01hero » Thu Oct 07, 2010 4:49 am

hello i am a newbie in this area to,

awesome tutorial's

well, maybe i should try it for my own....

thanks ...
hero01hero
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Oct 07, 2010 4:37 am

Re: PNG image -> SQLite DB -> imageview

Postby rockyknight007 » Fri Oct 08, 2010 11:42 am

Hello Expert
I tested the fruit list sample with Android2.2 and HTC Desire. And it worked fine without error.
And is there a sample code, when I click the list , show alertdialog. ?

Waiting for your help

Best Regards
rockyknight007
Freshman
Freshman
 
Posts: 4
Joined: Mon Oct 04, 2010 3:42 am

Re:

Postby oratis » Sat Nov 19, 2011 10:17 am

padde wrote:I found some minutes thanks to a canceled meeting :D
I changed the sample to a ListActivity and show a sample of presenting
the data from the database in a list.
If i understand it right thats what you want.

First the class representing a fruit:
Fruit.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package de.padde.fruitsdbsample;
  2.  
  3. import android.graphics.Bitmap;
  4.  
  5. public class Fruit {
  6.     private Bitmap bmp;
  7.     private String name;
  8.     private int kcal;
  9.     private int vitaminc;
  10.  
  11.     public Fruit(Bitmap b, String n, int k, int v) {
  12.         bmp = b;
  13.         name = n;
  14.         kcal = k;
  15.         vitaminc = v;
  16.     }
  17.    
  18.     public Bitmap getBitmap() { return bmp; }
  19.     public String getName() { return name; }
  20.     public int getKcal() { return kcal; }
  21.     public int getVitaminC() { return vitaminc; }
  22. }
  23.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

Nothing changed here...

Next is the class helping us handling the DB:
DBhelper.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package de.padde.fruitsdbsample;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4.  
  5. import android.content.ContentValues;
  6. import android.content.Context;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteOpenHelper;
  9. import android.graphics.Bitmap;
  10. import android.provider.BaseColumns;
  11.  
  12. public class DBhelper {
  13.     public static final String KEY_ID = BaseColumns._ID;
  14.     public static final String KEY_NAME = "name";
  15.     public static final String KEY_KCAL = "kcal";
  16.     public static final String KEY_VC = "vitaminc";
  17.     public static final String KEY_IMG = "image";
  18.    
  19.     private DatabaseHelper mDbHelper;
  20.     private SQLiteDatabase mDb;
  21.    
  22.     private static final String DATABASE_NAME = "FruitDB";
  23.     private static final int DATABASE_VERSION = 1;
  24.    
  25.     public static final String FRUITS_TABLE = "fruits";
  26.  
  27.     private static final String CREATE_FRUITS_TABLE = "create table "+FRUITS_TABLE+" ("
  28.                                          +KEY_ID+" integer primary key autoincrement, "
  29.                                          +KEY_IMG+" blob not null, "
  30.                                          +KEY_NAME+" text not null unique, "
  31.                                          +KEY_KCAL+" integer not null, "
  32.                                          +KEY_VC+" integer not null);";
  33.                                              
  34.     private final Context mCtx;
  35.     private boolean opened = false;
  36.    
  37.  
  38.     private static class DatabaseHelper extends SQLiteOpenHelper {
  39.         DatabaseHelper(Context context) {
  40.             super(context, DATABASE_NAME, null, DATABASE_VERSION);
  41.         }
  42.    
  43.         public void onCreate(SQLiteDatabase db) {
  44.             db.execSQL(CREATE_FRUITS_TABLE);
  45.         }
  46.    
  47.         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  48.             db.execSQL("DROP TABLE IF EXISTS "+FRUITS_TABLE);
  49.             onCreate(db);
  50.         }
  51.     }
  52.    
  53.     public void Reset() {
  54.         openDB();
  55.         mDbHelper.onUpgrade(this.mDb, 1, 1);
  56.         closeDB();
  57.     }
  58.    
  59.     public DBhelper(Context ctx) {
  60.         mCtx = ctx;
  61.         mDbHelper = new DatabaseHelper(mCtx);
  62.     }
  63.    
  64.     private SQLiteDatabase openDB() {
  65.         if(!opened)
  66.             mDb = mDbHelper.getWritableDatabase();
  67.         opened = true;
  68.         return mDb;
  69.     }
  70.    
  71.     public SQLiteDatabase getHandle() { return openDB(); }
  72.    
  73.     private void closeDB() {
  74.         if(opened)
  75.             mDbHelper.close();
  76.         opened = false;
  77.     }
  78.  
  79.     public void createFruitEntry(Fruit fruit) {
  80.         openDB();
  81.         ByteArrayOutputStream out = new ByteArrayOutputStream();
  82.         fruit.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, out);
  83.         ContentValues cv = new ContentValues();
  84.         cv.put(KEY_IMG, out.toByteArray());            
  85.         cv.put(KEY_NAME, fruit.getName());
  86.         cv.put(KEY_KCAL, fruit.getKcal());
  87.         cv.put(KEY_VC, fruit.getVitaminC());
  88.         mDb.insert(FRUITS_TABLE, null, cv);
  89.         closeDB();
  90.     }
  91. }
  92.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4

Nothing essential changed here...

Following class is new and defines how to bind the data to the Views.
Out of the box SimpleCursorAdapter supports TextView and ImageView but
images have to be in form of a image ressource or a URI. Therefore we need
this class to override default binding and/or define how to bind to other view types.
FruitViewBinder.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package de.padde.fruitsdbsample;
  2.  
  3. import android.database.Cursor;
  4. import android.graphics.BitmapFactory;
  5. import android.view.View;
  6. import android.widget.ImageView;
  7. import android.widget.RatingBar;
  8. import android.widget.SimpleCursorAdapter;
  9.  
  10. public class FruitViewBinder implements SimpleCursorAdapter.ViewBinder {
  11.         public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
  12.                
  13.                 if(view instanceof ImageView) {
  14.                         ImageView iv = (ImageView) view;
  15.                         byte[] img = cursor.getBlob(columnIndex);
  16.                         iv.setImageBitmap(BitmapFactory.decodeByteArray(img, 0, img.length));
  17.                         return true;
  18.                 }
  19.  
  20.                 if(view instanceof RatingBar) {
  21.                         RatingBar rb = (RatingBar) view;
  22.                         int kcal = cursor.getInt(cursor.getColumnIndex(DBhelper.KEY_KCAL));
  23.                         int vitc = cursor.getInt(cursor.getColumnIndex(DBhelper.KEY_VC));
  24.                         rb.setRating((float) (kcal * ((float) vitc/1000.0)));
  25.                         return true;
  26.                 }
  27.                
  28.                 return false;
  29.         }
  30. }
  31.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Last class is the ListActivity that implements the example
FruitDBSample
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package de.padde.fruitsdbsample;
  2.  
  3. import android.app.ListActivity;
  4. import android.database.Cursor;
  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;
  7. import android.os.Bundle;
  8. import android.widget.SimpleCursorAdapter;
  9.  
  10. public class FruitsDBSample extends ListActivity {
  11.     private DBhelper mDB;
  12.        
  13.     public void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.              
  16.         mDB = new DBhelper(this);
  17.        
  18.         mDB.Reset();
  19.        
  20.         Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
  21.        
  22.         mDB.createFruitEntry(new Fruit(img, "Banane",   92, 10));
  23.         mDB.createFruitEntry(new Fruit(img, "Kiwi",     56, 71));
  24.         mDB.createFruitEntry(new Fruit(img, "Pfirsich", 41, 10));
  25.         mDB.createFruitEntry(new Fruit(img, "Zitrone",  40, 51));
  26.        
  27.         String[] columns = {mDB.KEY_ID, mDB.KEY_IMG, mDB.KEY_NAME, mDB.KEY_KCAL, mDB.KEY_VC};
  28.         String   table   = mDB.FRUITS_TABLE;
  29.        
  30.         Cursor c = mDB.getHandle().query(table, columns, null, null, null, null, null);
  31.  
  32.         startManagingCursor(c);
  33.        
  34.         SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
  35.                 R.layout.fruitlist,
  36.                 c,
  37.                 new String[] {mDB.KEY_IMG, mDB.KEY_NAME, mDB.KEY_KCAL, mDB.KEY_VC},
  38.                 new int[] {R.id.img, R.id.txt, R.id.rating});
  39.        
  40.         adapter.setViewBinder(new FruitViewBinder());
  41.  
  42.         setListAdapter(adapter);
  43.     }
  44. }
  45.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


And last but not least the xml that defines the layout of a row in the final list
fruitlist.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="wrap_content">
  6.    
  7.     <ImageView
  8.        android:id = "@+id/img"
  9.        android:layout_width = "wrap_content"
  10.        android:layout_height = "wrap_content"
  11.        android:layout_alignParentLeft = "true">
  12.     </ImageView>
  13.    
  14.     <TextView
  15.        android:id = "@+id/txt"
  16.        android:layout_width = "wrap_content"
  17.        android:layout_height = "wrap_content"
  18.        android:layout_centerVertical = "true"
  19.        android:layout_toRightOf = "@id/img">
  20.     </TextView>
  21.    
  22.     <RatingBar
  23.        style="?android:attr/ratingBarStyleSmall"    
  24.        android:id = "@+id/rating"
  25.        android:numStars = "5"
  26.        android:stepSize = "0.5"
  27.        android:layout_width = "wrap_content"
  28.        android:layout_height = "wrap_content"
  29.        android:layout_alignParentRight = "true"
  30.        android:layout_centerVertical = "true"
  31.        android:layout_marginRight = "5px">
  32.     </RatingBar>
  33.    
  34.     <TextView
  35.        android:text = "Healthindex: "
  36.        android:layout_width = "wrap_content"
  37.        android:layout_height = "wrap_content"
  38.        android:layout_centerVertical = "true"
  39.        android:layout_toLeftOf = "@id/rating">
  40.     </TextView>
  41.    
  42. </RelativeLayout>
  43.  
Parsed in 0.005 seconds, using GeSHi 1.0.8.4


This is all pretty straight forward so no further explanation at this point.. but should something
remain unclear feel free to ask.

Greets Padde

--- Edit ---
And this is how it looks like.

Image




and what if I want to put an ImageButton instead of a ratingbar behind the view, shall I put it in the fruitviewbinder or shall I write a simplecursoradaptor and put the button there??
oratis
Freshman
Freshman
 
Posts: 5
Joined: Sat Nov 19, 2011 2:41 am

Re: PNG image -> SQLite DB -> imageview

Postby paul590 » Fri Mar 02, 2012 1:25 am

Thanks a lot for this tutorial, it has helped me a lot! but i have a question, i want to change the code in order to connect to a mysql database, is there easy way to do that and have the same result for the app? i been connecting to a mysql database and was successful in getting a string but my problem is how can I send more than one thing from the php script to the app? thanks again!
paul590
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Mar 02, 2012 1:17 am

Re: PNG image -> SQLite DB -> imageview

Postby rangasai » Mon Mar 26, 2012 9:56 am

hey padde
I have written the code just like for image-->SqliteDb-->imageview.But taking the listview sidebyside now i am not getting the onclick listener on listview.

just i will show what i have done

Textview image Textview Textview.
Textview image Textview Textview.
Textview image Textview Textview.
Textview image Textview Textview.

By clciking on the every Textview it will show the toastmessage which position we selected.I am new to android help me how to set onclicklistener for this one.
rangasai
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Mar 24, 2012 11:24 am

Top

Re: PNG image -> SQLite DB -> imageview

Postby jevrie10 » Sun Apr 08, 2012 4:38 pm

nice tutorial but how to implement onitemclick to get string from list item?
jevrie10
Freshman
Freshman
 
Posts: 2
Joined: Sat Jul 02, 2011 9:56 am

Re: PNG image -> SQLite DB -> imageview

Postby shal05 » Thu Jul 26, 2012 6:18 am

Firstly, thank you so much for the code padde, it really helped alot. I am also doing a similar app. But my app saves the recording that the user records using the microphone. For example, the user records a doorbell sound. He then saves it with the text saying its a doorbell and an image of the doorbell. I am not sure on how to save sound in the database. I am really new to android development.
shal05
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Jul 26, 2012 6:01 am

Top
Previous

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 5 guests