I’m writing an app that involves taking a photo, and then saving the URI of that photo in a database. The image URI makes up part of a single entry (an entry consisting of a photo and multiple audio file URI’s). Below is my code so far for this part of the application:
Database Handler
package com.example.HwST_GoMo;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.util.Log;
public class DatabaseHandler extends SQLiteOpenHelper {
private SQLiteDatabase myDataBase;
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 10;
// Database Name
private static final String DATABASE_NAME = "db";
private static final String DB_PATH = "/mnt/sdcard/HWST/";
// Contacts table name
private static final String TABLE_ENTRIES = "Entries";
// Contacts Table Columns names
private static final String KEY_ID = "entry_id";
private static final String KEY_IMAGE_URI = "image_uri";
private static final String KEY_AUDIO_URI = "audio_uri";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_ENTRIES + "("
+ KEY_AUDIO_URI + " TEXT, " + KEY_IMAGE_URI + " TEXT," + KEY_ID + " INTEGER PRIMARY KEY" +
")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ENTRIES);
// Create tables again
onCreate(db);
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DATABASE_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
}
public void addEntry(String URI){
//SQLiteDatabase db = this.getWritableDatabase();
myDataBase = this.getWritableDatabase();
ContentValues values = new ContentValues();
//values.put(KEY_IMAGE_URI, entry.getImageUri()); // Contact Name
//values.put(KEY_AUDIO_URI, entry.getAudioUri()); // Contact Phone Number
values.put(KEY_AUDIO_URI, "LOLOL");
values.put(KEY_IMAGE_URI, URI);
values.put(KEY_ID, 265);
// Inserting Row
long test = myDataBase.insert(TABLE_ENTRIES, null, values);
System.out.println("PLEASE REACH THIS also " + test);
this.close();
//myDataBase.close(); // Closing database connection
}
// Getting single entry
Entry getEntry(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_ENTRIES, new String[] { KEY_ID,
KEY_IMAGE_URI, KEY_AUDIO_URI }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Entry entry = new Entry(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
// return entry
return entry;
}
// Getting All Entries
public List<Entry> getAllEntries() {
List<Entry> entryList = new ArrayList<Entry>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_ENTRIES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Entry entry = new Entry();
entry.setID(Integer.parseInt(cursor.getString(0)));
entry.setImageUri(cursor.getString(1));
entry.setAudioUri(cursor.getString(2));
// Adding entry to list
entryList.add(entry);
} while (cursor.moveToNext());
}
// return entry list
return entryList;
}
// Updating single entry
public int updateEntry(Entry entry) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_IMAGE_URI, entry.getImageUri());
values.put(KEY_AUDIO_URI, entry.getAudioUri());
// updating row
return db.update(TABLE_ENTRIES, values, KEY_ID + " = ?",
new String[] { String.valueOf(entry.getID()) });
}
// Deleting single contact
public void deleteEntry(Entry entry) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_ENTRIES, KEY_ID + " = ?",
new String[] { String.valueOf(entry.getID()) });
db.close();
}
// Getting contacts Count
public int getEntriesCount() {
String countQuery = "SELECT * FROM " + TABLE_ENTRIES;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
TakePhotographPage
package com.example.HwST_GoMo;
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.content.ContextWrapper;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class TakePhotographPage extends Activity {
/** Called when the activity is first created. */
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
DatabaseHandler db = new DatabaseHandler(this);
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.takephotographpage);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
this.imageView = (ImageView)this.findViewById(R.id.photoResultView);
this.textView = (TextView)this.findViewById(R.id.textTest);
Button button = (Button)findViewById(R.id.back_to_menu);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), ControlsPage.class);
startActivityForResult(intent, 0);
// TODO Auto-generated method stub
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
int temp = (new File("/mnt/sdcard/HWST/").list().length) +1 ;
String fileName = Integer.toString(temp);
File file = null;
FileOutputStream os;
String dirName = "/HWST/";
try {
File sdCard = Environment.getExternalStorageDirectory();
System.out.println(sdCard);
File dir = new File (sdCard.getAbsolutePath() + dirName);
file = new File(dir, fileName + ".png");
System.out.println(file);
FileOutputStream outStream = new FileOutputStream(file);
photo.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
DatabaseHandler db = new DatabaseHandler(this);
db.openDataBase();
db.addEntry(file.toString());
}catch(Exception e){
System.out.println(e.toString());
}
//db.addEntry(new Entry(0, "" + u + "", "test"));
}
}
}
Entry Class
package com.example.HwST_GoMo;
public class Entry {
//private variables
int _id;
String _imageUri;
String _audioUri;
//Empty Constructor
public Entry(){
}
//constructor
public Entry(int id, String imageUri, String audioUri){
this._id = id;
this._imageUri = imageUri;
this._audioUri = audioUri;
}
//get ID
public int getID(){
return this._id;
}
//set ID
public void setID(int id){
this._id = id;
}
//get imageUri
public String getImageUri(){
return this._imageUri;
}
//set imageUri
public void setImageUri(String imageUri){
this._imageUri = imageUri;
}
//get audioUri
public String getAudioUri(){
return this._audioUri;
}
//set audioUri
public void setAudioUri(String audioUri){
this._audioUri = audioUri;
}
}
My problem is that nothing is being added to my database, instead it seems to be being added to a database in /data/data that I have no access to. If I set the KEY_ID to a number that I have not used before, then run the app and take a photo, logcat presents me with the following: http://img26.imageshack.us/img26/6526/logcat1.png
If I run the app again, using the same KEY_ID, I get the following:
http://img706.imageshack.us/img706/1857/logcat2.png
Using SQLite Database Browser on the PC and the SQLite Manager app on the phone, I see that nothing is being entered into the database.
The constraint error when entering the same ID for a second time leads me to believe that the data is going into a database (although it’s the wrong one), so my question is, what change do I make to force the app to put the entries into the database at “/mtn/sdcard/HWST/db” rather than the address at “/data/data/com.example.HwST_GoMo/databases/db”?
Sorry for having to use images for the logcat output, but it wasn’t allowing me to export the messages to a txt file.
The constructor will create the database at the place you specify. If the name starts with
/(an absolute path) then the database will be created there. If you give it something else then it will create it in/data/data/your.package.name/databases/[name]. That path is your apps home directory and it can read / write in there.also: you should move everything besides constructor / onCreate / onUpgrade from your
DatabaseHandlerin a new class and useDatabaseHandlerfrom there. The intention ofSQLiteOpenHelperis to help you get a writeable database that has the required tables (created / upgraded). You better do not use it for other things.