When to close database in ContentProvider

by Wah » Sun, 01 Mar 2009 16:10:59 GMT


Sponsored Links
 I am trying to write a ContentProvider which itself is based on a
SqlLite database. I am puzzled when I need to close the database
because ContentProvider has onCreate( ) function but not onDestroy( )
function.

I downloaded the android source code tree, and found
com.example.codelab.rssexample.RssContentProvider

It appears this class called "openDatabase" but never "close" on it:

=========
    @Override
    public boolean onCreate() {
        ...
        final Context con = getContext();
        try{
            mDb = mDbHelper.openDatabase(getContext(), DATABASE_NAME,
null, DB_V
ERSION);
        ...
     }

=========
mDb.close( ) is never called
=========

If there is no onDestroy() from ContentProvider and we don't call close
( ) on databases, doesn't this lead to some kind of memory/resource
leak?

Wah
--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Problem in getting Image URI(s) from camera

Hello,
Many people want this functionality, so do I. This is one of my
attempts. Can anyone guide me where am I going wrong...

I need to get the image URIs of all the images clicked by the camera
when it was running.
The basic logic I apply is this:

1] Before starting the camera, store a time stamp
                Date date = new Date();
                long milliDate = date.getTime();

2] Then I start the camera
                ComponentName camComp = new ComponentName
("com.android.camera", "com.android.camera.Camera");
                Intent takePic = new Intent();
                takePic.addCategory("android.intent.category.LAUNCHER");
                takePic.addFlags(MODE_PRIVATE);
                takePic.setAction("android.intent.action.MAIN");
                takePic.setComponent(camComp);
                startActivityForResult(takePic, CAM_ACTIVITY);

3] And finally, when it returns, I try to find out all the image URIs
which were clicked during this time period i.e. time > milliDate

        if(requestCode == CAM_ACTIVITY){
                Cursor imgCursor = managedQuery
(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                new 
String[]{android.provider.MediaStore.Images.Media._ID},
                                MediaStore.MediaColumns.DATE_ADDED + " > " + 
milliDate , null,
null);

                if(imgCursor != null){
                        imgCursor.moveToFirst();
                        int _IDidx = imgCursor.getColumnIndex
(android.provider.MediaStore.Images.Media._ID);
                        do{
                                String _ID = imgCursor.getString(_IDidx);
                                String content_URI =
MediaStore.Images.Media.EXTERNAL_CONTENT_URI + "/" + _ID;
                        }while(imgCursor.moveToNext());

                }

Some thing is going wrong here and I get
android.database.CursorIndexOutOfBoundsException for the line String
_ID = imgCursor.getString(_IDidx);
Can anyone tell me where I'm going wrong !!

Regards,
Tejas
--~--~---------~--~----~------------~-------~--~----~

2. IMF Articles

"One of the major new features we are introducing in Android 1.5 is
our Input Method Framework (IMF), which allows developers on-screen
input methods such as software keyboards."
http://android-developers.blogspot.com/2009/04/updating-applications-for-on-screen.html

This is a concise article that I'm much looking forward to reading.
However, with this sentence corrected everything might come together
more easily. I also had a hard time with the other article so this is
a tough subject. So, when do I want to use the IMF?


- juan
--~--~---------~--~----~------------~-------~--~----~

3. Apps updating update

4. webview transition

5. How to create and open an SQLite database? (Based on NotePad tutorial)

6. Soft keypad not appearing for edit text-Urgent

7. how to get href value of a html file loaded on webview