· 6 years ago · Mar 12, 2019, 12:58 PM
108-16 09:43:22.094: E/AndroidRuntime(383): Uncaught handler: thread main exiting due to uncaught exception
208-16 09:43:22.103: E/AndroidRuntime(383): java.lang.IllegalStateException: attempt to acquire a reference on a close SQLiteClosable
308-16 09:43:22.103: E/AndroidRuntime(383): at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:31)
408-16 09:43:22.103: E/AndroidRuntime(383): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:56)
508-16 09:43:22.103: E/AndroidRuntime(383): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
608-16 09:43:22.103: E/AndroidRuntime(383): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:49)
708-16 09:43:22.103: E/AndroidRuntime(383): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1221)
808-16 09:43:22.103: E/AndroidRuntime(383): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1194)
908-16 09:43:22.103: E/AndroidRuntime(383): at com.exa.receipt.ExaReceipts.populateDataForList(EReceipts.java:459)
1008-16 09:43:22.103: E/AndroidRuntime(383): at com.exa.receipt.ExaReceipts.access$0(ExaReceipts.java:397)
1108-16 09:43:22.103: E/AndroidRuntime(383): at com.exa.receipt.ExaReceipts$2.handleMessage(EReceipts.java:2053)
1208-16 09:43:22.103: E/AndroidRuntime(383): at android.os.Handler.dispatchMessage(Handler.java:99)
1308-16 09:43:22.103: E/AndroidRuntime(383): at android.os.Looper.loop(Looper.java:123)
1408-16 09:43:22.103: E/AndroidRuntime(383): at android.app.ActivityThread.main(ActivityThread.java:4363)
1508-16 09:43:22.103: E/AndroidRuntime(383): at java.lang.reflect.Method.invokeNative(Native Method)
1608-16 09:43:22.103: E/AndroidRuntime(383): at java.lang.reflect.Method.invoke(Method.java:521)
1708-16 09:43:22.103: E/AndroidRuntime(383): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
1808-16 09:43:22.103: E/AndroidRuntime(383): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
1908-16 09:43:22.103: E/AndroidRuntime(383): at dalvik.system.NativeStart.main(Native Method)
20
21
22
2308-16 09:08:49.893: E/Database(222): Leak found
2408-16 09:08:49.893: E/Database(222): java.lang.IllegalStateException: mPrograms size 1
2508-16 09:08:49.893: E/Database(222): at android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:1669)
2608-16 09:08:49.893: E/Database(222): at dalvik.system.NativeStart.run(Native Method)
2708-16 09:08:49.893: E/Database(222): Caused by: java.lang.IllegalStateException: /data/data/com.exa.receipt/databases/E_RECEIPTS_NEWDBG2.db SQLiteDatabase created and never closed
2808-16 09:08:49.893: E/Database(222): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1695)
2908-16 09:08:49.893: E/Database(222): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:739)
3008-16 09:08:49.893: E/Database(222): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:761)
3108-16 09:08:49.893: E/Database(222): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:754)
3208-16 09:08:49.893: E/Database(222): at android.app.ApplicationContext.openOrCreateDatabase(ApplicationContext.java:473)
3308-16 09:08:49.893: E/Database(222): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:193)
3408-16 09:08:49.893: E/Database(222): at com.exa.receipt.DBHandler.openDatabase(DBHandler.java:181)
3508-16 09:08:49.893: E/Database(222): at com.exa.receipt.ExaReceipts.onCreate(ExaReceipts.java:287)
3608-16 09:08:49.893: E/Database(222): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
3708-16 09:08:49.893: E/Database(222): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
3808-16 09:08:49.893: E/Database(222): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
3908-16 09:08:49.893: E/Database(222): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
4008-16 09:08:49.893: E/Database(222): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
4108-16 09:08:49.893: E/Database(222): at android.os.Handler.dispatchMessage(Handler.java:99)
4208-16 09:08:49.893: E/Database(222): at android.os.Looper.loop(Looper.java:123)
4308-16 09:08:49.893: E/Database(222): at android.app.ActivityThread.main(ActivityThread.java:4363)
4408-16 09:08:49.893: E/Database(222): at java.lang.reflect.Method.invokeNative(Native Method)
4508-16 09:08:49.893: E/Database(222): at java.lang.reflect.Method.invoke(Method.java:521)
4608-16 09:08:49.893: E/Database(222): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
4708-16 09:08:49.893: E/Database(222): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
4808-16 09:08:49.893: E/Database(222): at dalvik.system.NativeStart.main(Native Method)
49
50else
51 if(v == delete_btn){
52
53 if(current_checked_rows_ids.isEmpty()){
54 Toast.makeText(ExaReceipts.this, "No items selected to delete", Toast.LENGTH_LONG).show();
55 return;
56 }
57
58
59 //Confirm action -- delete rows and images from SD card in separate thread.
60
61 AlertDialog.Builder builder = new AlertDialog.Builder(this);
62 builder.setMessage("Are you sure you want to delete " + current_checked_rows_ids.size() + " item?").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
63
64 public void onClick(DialogInterface dialog, int id) {
65 myProgressDialog = ProgressDialog.show(ExaReceipts.this,"", "Deleting selected receipts n Please wait ...", true);
66 new Thread(new Runnable() {
67
68 public void run() {
69 ArrayList<String> imagestodelete = new ArrayList<String>();
70 String tempinternalpath = "";
71 try{
72 for (Iterator<Integer> iterator = current_checked_rows_ids.iterator(); iterator.hasNext();) {
73
74 int receiptid = iterator.next();
75 // Log("Deleting row with id: " + receiptid);
76
77 //Get the path of images on SD Card to delete. Delete only after db rows are deleted
78 String selquery = "SELECT RECEIPT_PATH,RECEIPT_INTERNAL_PATH FROM " + DBHandler.RECEIPTS_TABLE + " WHERE ID = " + receiptid;
79
80 //if(myDB == null || !myDB.isOpen()){
81 myDB = DBHandler.openDatabase(ExaReceipts.this, DBHandler.RECEIPT_DATABASE);
82 //}
83 Cursor c = myDB.rawQuery(selquery,null);
84 if(c != null && c.getCount() > 0){
85 c.moveToFirst();
86 //temppath = c.getString(c.getColumnIndex("RECEIPT_PATH"));
87 tempinternalpath = c.getString(c.getColumnIndex("RECEIPT_INTERNAL_PATH"));
88 c.close();//We know only 1 can exist as id are unique
89 }
90 //Log("Deleting row ...");
91 String query = "DELETE FROM "+ DBHandler.RECEIPTS_TABLE +" WHERE ID = " + receiptid;
92 //Also delete the image if exist
93 myDB.execSQL(query);
94 if(!tempinternalpath.equalsIgnoreCase("")){
95 imagestodelete.add(tempinternalpath);
96 }
97
98 if(myDB != null){
99 myDB.close();
100 }
101 }
102 // Log("imagestodelete ===> " + imagestodelete);
103 deleteUnusedImages(imagestodelete);
104
105 }catch(Exception e){
106 handler.sendEmptyMessage(MSG_DELETION_FAILED);
107 //Log("Caught while deleting rows" + e.getLocalizedMessage());
108 }finally{
109
110 handler.sendEmptyMessage(MSG_CLOSE_PROGRESS);
111 if(myDB != null){
112 myDB.close();
113 }
114
115 }
116
117 }
118 }).start();
119
120
121
122
123 }
124
125
126
127
128 private void deleteUnusedImages(ArrayList<String> imagestodelete) {
129
130
131
132 if(imagestodelete != null && !imagestodelete.isEmpty()){
133
134 for (int i = 0; i < imagestodelete.size(); i++) {
135 try{
136
137
138
139 String filename = new String(imagestodelete.get(i)).replaceFirst(getFilesDir() + "/", "");
140 //Log("filename ==> " + filename + "n");
141 deleteFile(filename);//deletion of internal files work only with fullname along with extension e.g. filename.jpg
142
143
144
145 }catch(Exception e){
146 //Log("Exception caught while deleting files" + e.getLocalizedMessage());
147 }
148 }
149
150 handler.sendEmptyMessage(MSG_DELETION_SUCCESS);
151
152 }
153
154 }
155
156
157 })
158 .setNegativeButton("No", new DialogInterface.OnClickListener() {
159 public void onClick(DialogInterface dialog, int id) {
160 dialog.cancel();
161 }
162 });
163 AlertDialog alert = builder.create();
164 alert.show();
165
166public class DBHandler{
167
168 public static final String RECEIPT_DATABASE = "E_RECEIPTS_NEWDBG2.db";
169 public static final String RECEIPTS_TABLE = "RECEIPTS_LIST_NEWDBG2";
170 public static final String TEMP_TABLE = "TEMP_TABLE";
171 public static final String LOGIN_TABLE = "LOGIN_INFO";
172
173 public static final int icon_width = 96;
174 public static final int icon_height = 96;
175
176 public static String INTERNET_DISCONNECTED_MESSAGE = "Could not find internet connection.";
177
178 static ProgressDialog myProgressDialog = null;
179 private static String TAG = "DBHandler";
180
181 static String dropUSERTableQuery = "DROP TABLE "+RECEIPTS_TABLE+ ";";
182 public static String createReceiptTableQuery = "CREATE TABLE IF NOT EXISTS "
183 + RECEIPTS_TABLE
184 + " ("
185
186 + "ID INTEGER PRIMARY KEY AUTOINCREMENT,"
187
188 + "ID_SERVER VARCHAR,"
189
190
191 + "RECEIPT_NAME VARCHAR,"
192 + "RECEIPT_SERVER_NAME VARCHAR,"
193 + "RECEIPT_PATH VARCHAR,"
194 + "RECEIPT_INTERNAL_PATH VARCHAR,"
195 + "RECEIPT_ICON BLOB,"
196 + "MODIFIED LARGEINT,"
197 + "LAST_SYNC LARGEINT,"
198 + "TASK_REQUEST_ID VARCHAR,"
199 + "TASK_REQUEST_STATUS VARCHAR,"
200 + "STATUS VARCHAR,"
201 + "EMAILED_BY VARCHAR,"
202 + "CATEGORY VARCHAR,"
203 + "SUBCATEGORY VARCHAR,"
204
205
206
207 + "INVOICE_DATE VARCHAR,"
208 + "DATE_PAID VARCHAR,"
209 + "DUE_DATE VARCHAR,"
210 + "BILLING_PERIOD_FROM VARCHAR,"
211 + "BILLING_PERIOD_TO VARCHAR,"
212 + "ACCOUNT_NO VARCHAR,"
213 + "PAYMENT_METHOD VARCHAR,"
214 + "COMPANY_NAME VARCHAR,"
215 + "INVOICE_ADDRESSED_TO VARCHAR,"
216 + "AMOUNT_PAYABLE VARCHAR,"
217 + "AMOUNT_PAID VARCHAR,"
218 + "PAYMENT_METHOD_DETAILS VARCHAR,"
219 + "MERCHANT VARCHAR,"
220 + "AMT_PAYABLE_CURRENCY VARCHAR,"
221 +"AMT_PAID_CURRENCY VARCHAR,"
222 +"CUSTOM_FIELD_NAME11 VARCHAR,"
223 +"CUSTOM_FIELD_NAME21 VARCHAR,"
224 +"CUSTOM_FIELD_NAME31 VARCHAR,"
225 +"CUSTOM_FIELD_NAME41 VARCHAR,"
226 +"CUSTOM_FIELD_NAME51 VARCHAR,"
227 +"CUSTOM_FIELD_VALUE11 VARCHAR,"
228 +"CUSTOM_FIELD_VALUE21 VARCHAR,"
229 +"CUSTOM_FIELD_VALUE31 VARCHAR,"
230 +"CUSTOM_FIELD_VALUE41 VARCHAR,"
231 +"CUSTOM_FIELD_VALUE51 VARCHAR"
232 + " );" ;
233
234
235 public static String createTempTableQuery = "CREATE TABLE IF NOT EXISTS "
236 + TEMP_TABLE
237 + " ("
238
239 + "ID INTEGER PRIMARY KEY AUTOINCREMENT,"
240
241 + "ID_SERVER VARCHAR,"
242
243 + "RECEIPT_NAME VARCHAR,"
244 + "RECEIPT_SERVER_NAME VARCHAR,"
245 + "RECEIPT_PATH VARCHAR,"
246 + "RECEIPT_INTERNAL_PATH VARCHAR,"
247 + "RECEIPT_ICON BLOB,"
248 + "MODIFIED LARGEINT,"
249 + "LAST_SYNC LARGEINT,"
250 + "TASK_REQUEST_ID VARCHAR,"
251 + "TASK_REQUEST_STATUS VARCHAR,"
252 + "STATUS VARCHAR,"
253 + "EMAILED_BY VARCHAR,"
254 + "CATEGORY VARCHAR,"
255 + "SUBCATEGORY VARCHAR,"
256
257
258
259 + "INVOICE_DATE VARCHAR,"
260 + "DATE_PAID VARCHAR,"
261 + "DUE_DATE VARCHAR,"
262 + "BILLING_PERIOD_FROM VARCHAR,"
263 + "BILLING_PERIOD_TO VARCHAR,"
264 + "ACCOUNT_NO VARCHAR,"
265 + "PAYMENT_METHOD VARCHAR,"
266 + "COMPANY_NAME VARCHAR,"
267 + "INVOICE_ADDRESSED_TO VARCHAR,"
268 + "AMOUNT_PAYABLE VARCHAR,"
269 + "AMOUNT_PAID VARCHAR,"
270 + "PAYMENT_METHOD_DETAILS VARCHAR,"
271 + "MERCHANT VARCHAR,"
272 + "AMT_PAYABLE_CURRENCY VARCHAR,"
273 +"AMT_PAID_CURRENCY VARCHAR,"
274 +"CUSTOM_FIELD_NAME11 VARCHAR,"
275 +"CUSTOM_FIELD_NAME21 VARCHAR,"
276 +"CUSTOM_FIELD_NAME31 VARCHAR,"
277 +"CUSTOM_FIELD_NAME41 VARCHAR,"
278 +"CUSTOM_FIELD_NAME51 VARCHAR,"
279 +"CUSTOM_FIELD_VALUE11 VARCHAR,"
280 +"CUSTOM_FIELD_VALUE21 VARCHAR,"
281 +"CUSTOM_FIELD_VALUE31 VARCHAR,"
282 +"CUSTOM_FIELD_VALUE41 VARCHAR,"
283 +"CUSTOM_FIELD_VALUE51 VARCHAR"
284 + " );" ;
285
286static String quickAddReceiptQuery =
287
288 "(RECEIPT_NAME, RECEIPT_PATH, RECEIPT_INTERNAL_PATH, RECEIPT_ICON, MODIFIED , LAST_SYNC) "
289 + " VALUES ("
290
291 + "?,"
292 + "?,"
293 + "?,"
294 + "?,"
295 + "?,"
296 + "?"
297
298+ ");";
299
300static String createLoginTableQuery = "CREATE TABLE IF NOT EXISTS "
301 + DBHandler.LOGIN_TABLE
302 + " ("
303
304 + "STATE INT," //1 -- logged in before
305 + "DATE LARGEINT,"
306 + "USERNAME_LOGGED_IN VARCHAR,"
307 + "PASSWORD_LOGGED_IN VARCHAR"
308
309 + " );" ;
310
311 static int global = 10;
312
313
314
315
316 public static SQLiteDatabase openDatabase(Context context, String DBname){
317 SQLiteDatabase myDB;
318 myDB = context.openOrCreateDatabase(DBname,Context.MODE_PRIVATE, null);
319 Cursor rs = null;
320 boolean tableExists;
321 try{
322 rs = myDB.rawQuery("SELECT * FROM " + RECEIPTS_TABLE + " WHERE 1=0", null );
323 tableExists = true;
324 }catch(Exception ex){
325 tableExists = false;
326 }finally{
327 if (rs != null) rs.close();
328 }
329 if(!tableExists) {
330 myDB.execSQL(createReceiptTableQuery);
331 }
332
333 return myDB;
334 }
335
336 public static void executeQuery(SQLiteDatabase db,String query){
337
338 if(db != null){
339 db.execSQL(query);
340 }
341
342 }
343
344 public static Cursor selectQuery(SQLiteDatabase db,String query){
345
346 if(db != null)
347 {
348 Cursor c = db.rawQuery(query,null);
349 return c;
350 }
351 else
352 return null;
353
354 }
355
356 public static void closeDatabase(SQLiteDatabase db){
357 if (db != null)
358 {
359 Log("Closing Database conn");
360 db.close();
361 }
362 }
363
364
365 /** Called when the activity is first created. */
366
367 public static boolean createNewDatabase(Context context, boolean overwrite) {
368
369 boolean tableExists = false;
370 //Check whether database and table exists
371
372 SQLiteDatabase myDB = null;
373 try {
374
375 myDB = openDatabase(context, RECEIPT_DATABASE);
376 Log("Database path ===> " + myDB.getPath());
377 if(overwrite)
378 {
379 /*if(!checkInternet(context)){
380 Toast.makeText(context, INTERNET_DISCONNECTED_MESSAGE,Toast.LENGTH_SHORT ).show();
381 return false;
382 }*/
383
384 try{
385 Log("Before executing drop table");
386 executeQuery(myDB,dropUSERTableQuery);
387
388 }
389 catch(Exception e){
390 Log("Exception in drop table");
391 }
392 }
393 else{
394 ////////********* Logic To Check if Table Already Exist**********///////////////
395
396 Cursor rs = null;
397 try{
398 rs = myDB.rawQuery("SELECT * FROM " + RECEIPTS_TABLE + " WHERE 1=0", null );
399 tableExists = true;
400
401 }catch(Exception ex){
402 tableExists = false;
403 }finally{
404 if (rs != null) rs.close();
405 }
406
407 //////****************************************///////////////
408 }
409
410 if(overwrite || !tableExists )
411 {
412 Log("Creating Table and writing database");
413 executeQuery(myDB,createReceiptTableQuery);
414
415 executeQuery(myDB, createTempTableQuery);
416 }
417
418 if(myDB != null){
419 myDB.close();
420
421 }
422
423
424 } catch (Exception e) {
425
426 Log("Exception thrown while accessing database");
427
428 } finally {
429 Log("Calling closeDatabase()");
430 closeDatabase(myDB);
431
432 }
433 Log("After writing to database");
434 return true;
435 }
436
437 //ID,RECEIPT_NAME,RECEIPT_IMAGE,RECEIPT_ICON,MODIFIED
438 static boolean updateDatabaseRow(Context context, SQLiteDatabase database, File filepath, InputStream inputfilestream, String imagetype) {
439
440 boolean insertionSuccess = false;
441
442 Cursor last_id_cur = selectQuery(database, "SELECT ID FROM " + DBHandler.RECEIPTS_TABLE + " ORDER by ID");
443
444
445 // final DateFormat formatter = new SimpleDateFormat("ddMMyyyyhhmm");
446
447
448
449 //Log("Date-" + date);
450
451 int last_id = 0;
452
453 if( (last_id_cur != null ) && (last_id_cur.getCount() > 0) ) {
454
455 last_id_cur.moveToLast();
456 last_id = last_id_cur.getInt(0);
457 }
458
459 last_id_cur.close();
460 Log("last_id is =============>>>" + last_id);
461
462 /****************************************************** CHANGED ******************************/
463
464 //String new_id = "test";
465
466
467 Date d = new Date();
468 final String date_name = (new SimpleDateFormat("ddMMyyyyHHmmss")).format(d);
469
470 /////////////////Start of Writing file to internal memory //////////////////
471
472 String FILENAME = "Receipt"+ date_name + imagetype;
473
474 FileOutputStream fos;
475 try {
476 //File tempFile = new File( context.getDir("temp" , Context.MODE_WORLD_WRITEABLE), "image.tmp");
477
478 InputStream in = new FileInputStream(filepath);
479 fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
480 byte[] buf = new byte[1024];
481 int len;
482 while ((len = in.read(buf)) > 0){
483 fos.write(buf, 0, len);
484 }
485 in.close();
486 fos.close();
487 Log("File " + FILENAME + " copied in internal memory");
488 }catch(FileNotFoundException e) {
489 Log("Caught1 in writing image in internal storage" + e.getLocalizedMessage());
490 return false;
491 }catch(IOException e) {
492 Log("Caught2 in writing image in internal storage" + e.getLocalizedMessage());
493 return false;
494 }catch(Exception e) {
495 Log("Caught3 in writing image in internal storage" + e.getLocalizedMessage());
496 return false;
497 }