How to create Android Contact Book App : Part 7 : Deleting Contact Details from SQLite Database

In this part, we will cover how to delete contact details from SQLite Contact Database.
Android Deleting Contact Details from SQLite Database
Open “ContactHandler.java” file from previous part and add following function to delete contact details by contact id.

Open “ContactDetails.java” file and define ContactHandler named “handler”.

Now add the following code snippet under “case 2” into “onOptionsItemSelected ()” function.

Complete “ContactDetails.java”

I hope, you like this tutorial with 7 parts.
Download the final source of the Tutorial.

14 replies
  1. Algarrobo
    Algarrobo says:

    Hi! I get the following error when editing a contact:
    E/BitmapFactory(1664): Unable to decode stream: java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory)

    For example I edit the name of a contact, save it, and then the image disappears. The rest works just fine.

    What am I doing wrong?

    Thanks for the tutorial, by the way.

    Reply
    • Ajay Bagujar
      Ajay Bagujar says:

      Hi Algarrobo,

      Make sure image must be there on correct place and correct name which is stored in database column.

      The error is generating because BitmapFactory class is unable find the image which you are providing (ie. name or path)

      Reply
      • Algarrobo
        Algarrobo says:

        Hi! Thanks for your answer, I had not seen it before.
        I’ve checked that the image is on its correct place.
        The problem is when I update a contact. If I update ie. only the name, the image dissappears, and I get an error.
        But if I update the name, and the image (and put the same image or any other) then there is no problem. Is like it was compulsory to change the image before updating the contact.

        Reply
        • chris
          chris says:

          Hi,
          first of amazing tutorial, thankyou. Really has helped me. However I am having the same problem as Algarrobo. Is there a solution?

          Reply
          • Ajay Bagujar
            Ajay Bagujar says:

            Hi chris,
            Thanks for putting comments.

            As Algarrobo said, it is like compulsory to change the image before update, You may assign validation for the all fields. If there is no image selected or no date entered in field. You can ask for user to select image and enter text.

          • Algarrobo
            Algarrobo says:

            Finally I managed to fix it!
            I had to add this:
            picturePath = extras.getString(“photograph”);
            in “OnCreate”.
            This way onCreate sets the previous image, and if you update it gets changed.

            Thanks a lot for this great tutorial!!

  2. Akas
    Akas says:

    i was tried this code but i got bug in when add new contact image there is doesn’t appear profile picture please help me , why am getting this error?

    Reply
  3. pisiorek
    pisiorek says:

    Hello Ajar.
    You made fantastic work for every person who starts learning Android. I have one problem. When I want add picture to the contact – aplication stop. The same situation is when I want add or change picture from Edit level. Where can be problem ?
    Thank you for your help.

    Reply
      • pisiorek
        pisiorek says:

        Debug code:

        04-18 07:33:41.711 15329-15329/com.ajaybadgujar.contactbook W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41743da0)
        04-18 07:33:41.721 15329-15329/com.ajaybadgujar.contactbook E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.ajaybadgujar.contactbook, PID: 15329
        java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/11730 (has extras) }} to activity {com.ajaybadgujar.contactbook/com.ajaybadgujar.contactbook.NewContact}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/11730 from pid=15329, uid=10178 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3574)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
        at android.app.ActivityThread.access$1400(ActivityThread.java:169)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1325)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5476)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/11730 from pid=15329, uid=10178 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
        at android.os.Parcel.readException(Parcel.java:1472)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
        at android.content.ContentProviderProxy.query(ContentProviderNative.java:413)
        at android.content.ContentResolver.query(ContentResolver.java:464)
        at android.content.ContentResolver.query(ContentResolver.java:407)
        at com.ajaybadgujar.contactbook.NewContact.onActivityResult(NewContact.java:108)
        at android.app.Activity.dispatchActivityResult(Activity.java:5643)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3570)
                    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
                    at android.app.ActivityThread.access$1400(ActivityThread.java:169)
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1325)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:136)
                    at android.app.ActivityThread.main(ActivityThread.java:5476)
                    at java.lang.reflect.Method.invokeNative(Native Method)
                    at java.lang.reflect.Method.invoke(Method.java:515)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                    at dalvik.system.NativeStart.main(Native Method)

        Thank’s a lot.

        Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *