Android; Sqliteconnection Object For Database Was Leaked Even Closed
In my database helper I have both closed the cursor and database connection, but still the logcat says there's a leak! I have this code in my SQLiteOpenHelper // Getting all entrie
Solution 1:
After many frustrations I decided to change the whole code inside the db helper:
package net.amawal.android;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import net.amawal.android.models.Post;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class AmawalDB extends SQLiteOpenHelper {
// DATABASE
private static SQLiteDatabase mSqliteDb;
private static AmawalDB mInstance;
public static final int DATABASE_VERSION = 1;
private static final String DB_PATH_SUFFIX = "/databases/";
private static final String DATABASE_NAME = "amawal.sqlite";
private static Context mContext;
// Awals table post_title
private static final String wp_posts = "wp_posts";
public AmawalDB(Context context, CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, version);
mContext = context;
}
public void initialise() {
if (mInstance == null) {
if (!checkDatabase()) {
copyDataBase();
}
mInstance = new AmawalDB(mContext, null, DATABASE_VERSION);
mSqliteDb = mInstance.getWritableDatabase();
}
}
public AmawalDB getInstance(){
return mInstance;
}
public SQLiteDatabase getDatabase() {
return mSqliteDb;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private static void copyDataBase() {
try {
// Open your local db as the input stream
InputStream myInput = mContext.getAssets().open(DATABASE_NAME);
// Path to the just created empty db
String outFileName = getDatabasePath();
// if the path doesn't exist first, create it
File f = new File(mContext.getApplicationInfo().dataDir + DB_PATH_SUFFIX);
if (!f.exists())
f.mkdir();
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static boolean checkDatabase() {
SQLiteDatabase checkDB = null;
try {
try {
String myPath = getDatabasePath();
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
checkDB.close();
} catch (Exception e) { }
} catch (Throwable ex) {
}
return checkDB != null ? true : false;
}
private static String getDatabasePath() {
return mContext.getApplicationInfo().dataDir + DB_PATH_SUFFIX
+ DATABASE_NAME;
}
// Getting All Contacts
public List<Post> getAllPosts() {
List<Post> posts_list = new ArrayList<Post>();
// Select All Query
String selectQuery = "SELECT * FROM " + wp_posts + " ORDER BY post_title COLLATE NOCASE";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Post post = new Post();
post.setID(Integer.parseInt(cursor.getString(1)));
post.setPostContent(cursor.getString(2));
post.setpostTitle(cursor.getString(3));
// Adding post to list
posts_list.add(post);
} while (cursor.moveToNext());
}
// return posts list
return posts_list;
}
}
Post a Comment for "Android; Sqliteconnection Object For Database Was Leaked Even Closed"