Hey guys I wanted to save new image uploaded by user into SQLite database and show all images uploaded in list view. The rest of the items in SQLite had no problem getting different titles, captions etc from database except for image.
The problem is when the user first uploads an image it works fine, but subsequent uploaded images will update the previous images to the latest image. This cause all images to be the same as all previous images are updated to latest image).
What is the cause of this? The way I save and retrieve from SQLite is I'll convert Uri to String then save as TEXT in SQLite database, when I retrieve it I will convert String back to Uri.
One thing I've realised using the debugger is when I convert Uri to String, everytime my String will be the same: file:///data/data/packagename/cache/cropped.
Is this the reason why all my images are the same? Or is there nothing to do with Uri being the same?
public class TableDatabase extends SQLiteOpenHelper {
public String query = "CREATE TABLE " + TableData.TableInfo.TABLE_NAME
+ " (" + TableData.TableInfo.SMILEY + " INTEGER NOT NULL, "
+ TableData.TableInfo.TITLE + " TEXT, " + TableData.TableInfo.DATE
+ " TEXT, " + TableData.TableInfo.CAPTION + " TEXT, "
+ TableData.TableInfo.IMAGE + " TEXT);";
public TableDatabase(Context context) {
super(context, TableData.TableInfo.DATABASE_NAME, null,
TableData.TableInfo.DATABASE_VERSION);
// Check if database is created
Log.d("Database operations", "Database created");
}
@Override
public void onCreate(SQLiteDatabase db) {
// Create table
db.execSQL(query);
Log.d("Database operations", "Table created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
// Insert user information into the database
public void putInformation(TableDatabase data, int smiley, String title,
String date, String caption, String image) {
// Write data into database
SQLiteDatabase sqLiteDatabase = data.getWritableDatabase();
ContentValues contentValues = new ContentValues();
// Add value from each column into contentvalue
contentValues.put(TableData.TableInfo.SMILEY, smiley);
contentValues.put(TableData.TableInfo.TITLE, title);
contentValues.put(TableData.TableInfo.DATE, date);
contentValues.put(TableData.TableInfo.CAPTION, caption);
contentValues.put(TableData.TableInfo.IMAGE, image);
// Insert into sqlite database
sqLiteDatabase.insert(TableData.TableInfo.TABLE_NAME, null,
contentValues);
Log.d("Database operations", "One row inserted");
}
// Retrieve data from database
public Cursor getInformation(TableDatabase data) {
// Read data from sqlite database
SQLiteDatabase sqLiteDatabase = data.getReadableDatabase();
String[] columns = { TableData.TableInfo.SMILEY,
TableData.TableInfo.TITLE, TableData.TableInfo.DATE,
TableData.TableInfo.CAPTION, TableData.TableInfo.IMAGE };
// Points to first row of table
return sqLiteDatabase.query(TableData.TableInfo.TABLE_NAME, columns,
null, null, null, null, null);
}
}
Aucun commentaire:
Enregistrer un commentaire