saving to a SQLite database<\/a>. <\/p>\n\r\npublic class ApplicationDatabaseHelper extends SQLiteOpenHelper {\r\n\/**\r\n * Updates the current picture for the report.\r\n *\r\n * @param reportId the identifier of the report for which to save the picture\r\n * @param picture the picture to save to the internal storage and save path in the database.\r\n *\/\r\npublic void updateReportPicture(long reportId, Bitmap picture) {\r\n \/\/ Saves the new picture to the internal storage with the unique identifier of the report as \r\n \/\/ the name. That way, there will never be two report pictures with the same name.\r\n String picturePath = \"\";\r\n File internalStorage = mContext.getDir(\"ReportPictures\", Context.MODE_PRIVATE);\r\n File reportFilePath = new File(internalStorage, reportId + \".png\");\r\n String picturePath = reportFilePath.toString();\r\n\r\n FileOutputStream fos = null;\r\n try {\r\n fos = new FileOutputStream(reportFilePath);\r\n picture.compress(Bitmap.CompressFormat.PNG, 100 \/*quality*\/, fos);\r\n fos.close();\r\n }\r\n catch (Exception ex) {\r\n Log.i(\"DATABASE\", \"Problem updating picture\", ex);\r\n picturePath = \"\";\r\n }\r\n\r\n \/\/ Updates the database entry for the report to point to the picture\r\n SQLiteDatabase db = getWritableDatabase();\r\n\r\n ContentValues newPictureValue = new ContentValues();\r\n newPictureValue.put(ReportContract.ReportEntry.COLUMN_PICTURE_TITLE, \r\n picturePath);\r\n\r\n db.update(ReportContract.TABLE_NAME,\r\n newPictureValue,\r\n ReportContract.ReportEntry._ID + \"=?\",\r\n new String[]{String.valueOf(reportId)});\r\n }\r\n}\r\n<\/pre>\nIf you save a picture to the database, you also need a way to get that picture to display it in your application. Here is how to get the image that was just saved to display it again :<\/p>\n
\r\npublic class ApplicationDatabaseHelper extends SQLiteOpenHelper {\r\n\/**\r\n * Gets the picture for the specified report in the database.\r\n *\r\n * @param reportId the identifier of the report for which to get the picture.\r\n *\r\n * @return the picture for the report, or null if no picture was found.\r\n *\/\r\npublic Bitmap getReportPicture(long reportId) {\r\n String picturePath = getReportPicturePath(reportId);\r\n if (picturePath == null || picturePath.length() == 0)\r\n return (null);\r\n\r\n Bitmap reportPicture = BitmapFactory.decodeFile(picturePath);\r\n\r\n return (reportPicture);\r\n }\r\n\r\n\/**\r\n * Gets the path of the picture for the specified report in the database.\r\n *\r\n * @param reportId the identifier of the report for which to get the picture.\r\n * \r\n * @return the picture for the report, or null if no picture was found.\r\n *\/\r\nprivate String getReportPicturePath(long reportId) {\r\n \/\/ Gets the database in the current database helper in read-only mode\r\n SQLiteDatabase db = getReadableDatabase();\r\n\r\n \/\/ After the query, the cursor points to the first database row\r\n \/\/ returned by the request\r\n Cursor reportCursor = db.query(ReportContract.TABLE_NAME,\r\n null,\r\n ReportContract.ReportEntry._ID + \"=?\",\r\n new String[]{String.valueOf(reportId)},\r\n null,\r\n null,\r\n null);\r\n reportCursor.moveToNext();\r\n \r\n \/\/ Get the path of the picture from the database row pointed by\r\n \/\/ the cursor using the getColumnIndex method of the cursor.\r\n String picturePath = reportCursor.getString(reportCursor.\r\n getColumnIndex(ReportContract.ReportEntry.COLUMN_PICTURE_TITLE));\r\n\r\n return (picturePath);\r\n }\r\n}\r\n<\/pre>\nFinally, you need to make sure to delete the picture from the internal storage if you delete the associated record from the database. If you keep the pictures, the internal storage used by your application will grow larger with time and your users won’t have any way to delete those pictures since the internal storage is hidden from them.<\/p>\n
\r\npublic class ApplicationDatabaseHelper extends SQLiteOpenHelper {\r\n\/**\r\n * Deletes the specified report from the database, removing also the associated picture from the\r\n * internal storage if any.\r\n *\r\n * @param reportId the report to remove.\r\n *\/\r\npublic void deleteReport(long reportId) {\r\n \/\/ Remove picture for report from internal storage\r\n String picturePath = getReportPicturePath(reportId); \/\/ See above\r\n if (picturePath != null && picturePath.length() != 0) {\r\n File reportFilePath = new File(picturePath);\r\n reportFilePath.delete();\r\n }\r\n\r\n \/\/ Remove the report from the database\r\n SQLiteDatabase db = getWritableDatabase();\r\n\r\n db.delete(ReportContract.TABLE_NAME,\r\n ReportContract.ReportEntry._ID + \"=?\",\r\n new String[]{String.valueOf(reportId)});\r\n }\r\n}\r\n<\/pre>\n