0

Hello everyone i've been following this tutorial: http://coenraets.org/blog/android-samples/androidtutorial/ . I got what i needed from it , but now i have reached a problem. Basically , what i need is to be able to add a picture for each employee. I want a thumbnail picture on the list created and a full sized photo on the details page. How can i do this?

I tried to add a Photo marker in the database

String sql = "CREATE TABLE IF NOT EXISTS employee (" +
                        "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                        "firstName TEXT, " +
                        "lastName TEXT, " +
                        "title TEXT, " +
                        "officePhone TEXT, " +
                        "cellPhone TEXT, " +
                        "email TEXT, " +
                        photo IMAGE," + "managerId INTEGER)";
        db.execSQL(sql);

then i added values.put("photo", "@drawable/icon");.

Then in EmployeeDetails i added this line :

photo = (ImageView) findViewById(R.id.image);
photo.setTag(cursor.getString(cursor.getColumnIndex("photo")));

Of course i imported ImageView and protected ImageView photo; and for the Details layout i added:

<ImageView
        android:id="id/image
        android:layout_width="wrap_content"
        android:layout_heigh=wrap_content

Ask if i didn't managed to make myself clear enough. I was trying to put a photo for the details section of the employee I wasn't trying to make a thumbnail image yet the thing is , i did the whole thing and it came out without errors but when i run it , the app crashes.

user
  • 86,916
  • 18
  • 197
  • 190
FirstLaw
  • 73
  • 2
  • 10
  • use blob in your sql for storing images.. – Anand Tiwari Feb 09 '12 at 13:15
  • @AnandTiwari Sorry , but how do i do that? – FirstLaw Feb 09 '12 at 16:26
  • @AnandTiwari plus i dont mind using Drawable images , I just want a way to link an image to an item in the database – FirstLaw Feb 09 '12 at 16:33
  • in case of drawable, images are use by id reference which are auto generated. In this way you should go with assets manager, put your images in assets folder, and in database store relative path. so when when you want your image on View just get image path from db and image through assets folder using path. – Anand Tiwari Feb 10 '12 at 05:55
  • Okay i like this idea so, ill save the photos in the assers folder then in my db, ill add the path would the path then be @assets/photo and ill have to identify it in db ill try that out and get back to u thanks alot!!!!! – FirstLaw Feb 10 '12 at 08:53
  • i tried this image = (ImageView) findViewById(R.id.Image); image.setTag(cursor.getString(cursor.getColumnIndex("image"))); and i added emp.image... the app is still crashing... the image is in the assets folder , now what??? – FirstLaw Feb 12 '12 at 18:57
  • Uncaught handler: thread main exiting due to uncaught exception java.lang.RuntimeException: Unable to start activity ComponentInfo{com.firstlaw.omo/com.firstlaw.omo.Cook_tab_snacks_details}: android.database.sqlite.SQLiteException: no such column: emp.image: , while compiling: SELECT emp._id, emp.firstName, emp.lastName,emp.image, emp.title, emp.officePhone, emp.cellPhone, emp.email, emp.managerId, mgr.firstName managerFirstName, mgr.lastName managerLastName FROM employee emp LEFT OUTER JOIN employee mgr ON emp.managerId = mgr._id WHERE emp._id = ? – FirstLaw Feb 13 '12 at 14:11
  • at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) at android.app.ActivityThread.access$2200(ActivityThread.java:119) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4363)at java.lang.reflect.Method.invokeNative(Native Method) – FirstLaw Feb 13 '12 at 14:12
  • at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) at dalvik.system.NativeStart.main(Native Method) – FirstLaw Feb 13 '12 at 14:12
  • Caused by: android.database.sqlite.SQLiteException: no such column: emp.image: , while compiling: SELECT emp._id, emp.firstName, emp.lastName,emp.image, emp.title, emp.officePhone, emp.cellPhone, emp.email, emp.managerId, mgr.firstName managerFirstName, mgr.lastName managerLastName FROM employee emp LEFT OUTER JOIN employee mgr ON emp.managerId = mgr._id WHERE emp._id = ? at android.database.sqlite.SQLiteProgram.native_compile(Native Method) – FirstLaw Feb 13 '12 at 14:12
  • at android.database.sqlite.SQLiteProgram.compile(SQLiteProgram.java:110) at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:59) at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:49) at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:49) at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1221) at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1194) – FirstLaw Feb 13 '12 at 14:13
  • at com.firstlaw.omo.Cook_tab_snacks_details.onCreate(Cook_tab_snacks_details.java:28) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) this is the log cat , the error log shows nothing – FirstLaw Feb 13 '12 at 14:13
  • I think you have problem with sqlite code, you should check it.. – Anand Tiwari Feb 13 '12 at 14:14

1 Answers1

0

using blob storing and retriving image from database, this would give a better idea:

you also store assets folder path in db as text and use it to retrieve path from sql: then your sql:

String sql = "CREATE TABLE IF NOT EXISTS employee (" +
                    "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                    "firstName TEXT, " +
                    "lastName TEXT, " +
                    "title TEXT, " +
                    "officePhone TEXT, " +
                    "cellPhone TEXT, " +
                    "email TEXT, " +
                    "photo Text," + 
                    "managerId INTEGER)";
    db.execSQL(sql);

then you can get drawable from assets folder by:

Drawable d = Drawable.createFromStream(getAssets().open("path_from_db"), null);
img.setBackgroundResource(d);
Community
  • 1
  • 1
Anand Tiwari
  • 1,583
  • 10
  • 22
  • `` `String sql = "CREATE TABLE IF NOT EXISTS employee (" + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "firstName TEXT, " + "lastName TEXT, " + "image TEXT, " + "title TEXT, " + "officePhone TEXT, " + "cellPhone TEXT, " + "email TEXT, " + "managerId INTEGER)"; db.execSQL(sql);` `values.put("firstName", "John"); values.put("lastName", "Smith"); values.put("image","@assets/cook_default";....etc etc` – FirstLaw Feb 13 '12 at 15:05
  • i'm sorry about the format , i dont know what happend I think my main problem is this here ... `image = (ImageView) findViewById(R.id.image); image.setTag(cursor.getString(cursor.getColumnIndex("image"))); ` getColumnIndex doesn't work with Image View .... but i have no idea how to get it to go into my database and retrieve my image from assets folder – FirstLaw Feb 13 '12 at 15:08
  • you are right you dont have any column with name image, look at above you are using there photo, in create table statement... – Anand Tiwari Feb 14 '12 at 05:27
  • alright i got it all fixed , now ... it doesn't crash ... it shows nothing ... thanks for all your help again what do i do now ???.... – FirstLaw Feb 14 '12 at 07:32
  • Photo = (ImageView) findViewById(R.id.Photo); Photo.setTag(cursor.getString(cursor.getColumnIndexOrThrow("photo"))); Basically this command doesn't use the link in my data ... I fixed my database to point out the location of the picture , in my case its @assets/stub I even made each entry to point out to a diffrent variaion of that like , assets.stub , even made a drawable( copied stub to drawables) R.drawable.stub , and @drawable/stub , even @assets.stub, basically tried it all... `Photo.setTag(cursor.getString(cursor.getColumnIndexOrThrow("photo")));` <-- this isn't correct – FirstLaw Feb 14 '12 at 08:04
  • but what can i put instead... how can i ask it to take whats in the column and use it to show the image at the location... – FirstLaw Feb 14 '12 at 08:05
  • you should look in [here](http://stackoverflow.com/questions/1933015/opening-an-image-file-inside-the-assets-folder) and update answer – Anand Tiwari Feb 14 '12 at 08:16
  • `Photo = (ImageView) findViewById(R.id.Photo); try { InputStream bitmap=getAssets().open(cursor.getString(cursor.getColumnIndexOrThrow("photo"))); Bitmap bit=BitmapFactory.decodeStream(bitmap); Photo.setImageBitmap(bit); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }` and then i put stub.png in my database for photo column and it worked THANKS ALOT!!!!!!! CHEERS MATE! you are a life saver!!! – FirstLaw Feb 14 '12 at 08:51