Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7702857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:19:27+00:00 2026-05-31T23:19:27+00:00

I’m writing an app that involves taking a photo, and then saving the URI

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:19:28+00:00Added an answer on May 31, 2026 at 11:19 pm
    // Database Name
    private static final String DATABASE_NAME = "db";
    
    // path to db
    private static final String DB_PATH = "/mnt/sdcard/HWST/";
    
    // now both combined
    private static final String DB_FILE = DB_PATH + DATABASE_NAME;
    
    public DatabaseHandler(Context context) {
        super(context, DB_FILE, null, DATABASE_VERSION);
    }
    

    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 DatabaseHandler in a new class and use DatabaseHandler from there. The intention of SQLiteOpenHelper is to help you get a writeable database that has the required tables (created / upgraded). You better do not use it for other things.

    class TheRealDatabaseHandler {
    
        public void addEntry(String URI){
            DatabaseHandler helper = new DatabaseHandler(mContext);
            SQLiteDatabase db = helper.getWritableDatabase();
            db.insert...
       }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am writing an app with both english and french support. The app requests
I'm trying to create an if statement in PHP that prevents a single post
I am using Paperclip to handle profile photo uploads in my app. They upload
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.