Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
tomado
Posts: 6
Registered: ‎02-08-2011
My Device: Not Specified
Accepted Solution

Problem opening a database from SDCard that has been encrypted

Hi,

 

I'm having some trouble trying to open a connection to a sqlite database stored on the SDCard. I'm transferring the database file over the network when the application is first run, and if the phone is set to encrypt media card files then this gets written with the '.rem' suffix. Then I try to open a connection to the database using the following:

 

 

DatabaseFactory.open(dbPath)

dbPath here is a URI pointing to '/SDCard/data/database.db' which is the file which I have downloaded over the network. With encryption turned off this works fine and I can open the database and run queries on it etc. However with encryption on, I get the following exception:

 

net.rim.device.api.database.DatabaseIOException: File not found

 

I have tried changing it to first look for 'database.db.rem' in case of encryption but then receive this error:

 

net.rim.device.api.database.DatabaseIOException: Error decrypting header

 

I'm not sure how to get round this as I can't find a way to override the encryption settings on the phone and write the database file to the SD card without encryption. Or perhaps is there a way I can decrypt the database file after writing it if necessary?

 

Does anyone have any ideas on a way to solve this?


Thanks

 

Please use plain text.
New Developer
tomado
Posts: 6
Registered: ‎02-08-2011
My Device: Not Specified

Re: Problem opening a database from SDCard that has been encrypted

Just wondering if anyone had any thoughts on this? It's proving to be quite a problem!

Please use plain text.
Developer
shnuffy
Posts: 268
Registered: ‎08-19-2009
My Device: Bold 9700

Re: Problem opening a database from SDCard that has been encrypted

[ Edited ]

I am also having this problem. It seems silly that an application can't use it's own DB just because the device is encrypted. This must be a common case.

 

Any ideas?

Please use plain text.
Retired
mwoolley
Posts: 571
Registered: ‎06-25-2010
My Device: Z10

Re: Problem opening a database from SDCard that has been encrypted

This is a known issue. Fortunately the following workaround should resolve the issue for you:

 

Instead of download the .db file and then opening it directly in SQLite using the DatabaseFactory class, proceed as follows:

 

1.  Create the database(file) using the DatabaseFactory class.
2.  Close the database.
3.  Open a file connection to the database file that was created in step 1 and 2
4.  Truncate the file to 0 by calling FileConnection.truncate(0)
5.  Download the database over the network and write out the data to the file created above using FileConnection.openOutputStream()
6.  Close the file connection
7.  Now, re-open the database using the DatabaseFactory class and your SQL should work as expected.

 

Creating the database file using the DatabaseFactory class results in the file *not* being marked as requiring encryption.

 

Hope this works for you.

--------------------------------------------------------------------------------------------
Feel free to press the like button on the right side if you liked my attempts to help :-)
And please mark posts as solved if you think I found the solution or set you on its path. Thanks!
Follow me on Twitter: @mdwrim
Please use plain text.
Developer
shnuffy
Posts: 268
Registered: ‎08-19-2009
My Device: Bold 9700

Re: Problem opening a database from SDCard that has been encrypted

[ Edited ]

Thanks for the reply, kudos. We'll be implementing your fix.

Please use plain text.
New Developer
tomado
Posts: 6
Registered: ‎02-08-2011
My Device: Not Specified

Re: Problem opening a database from SDCard that has been encrypted

Great, just tried out the fix and it works perfectly.

Thanks for your help.

Please use plain text.
Regular Contributor
8vius
Posts: 52
Registered: ‎10-07-2010
My Device: BlackBerry Curve 8520

Re: Problem opening a database from SDCard that has been encrypted

[ Edited ]

Can anyone give me a hand i have this same problem and i'm getting an exception, maybe i'm doing it wrong, this is a snippet of my code: 

 

URI uri = URI.create(dbLocation + BD_NAME);     
            
            // Esto abre o crea la base de datos en el URI especificado
            Database db = DatabaseFactory.openOrCreate(uri);  
            
            // Se cierra la BD en caso de estar en blanco para poder escribir a ella
            db.close();
            
            // Se abre la conexión al archivo de la BD      
            FileConnection fileConnection = (FileConnection)Connector.open("file://" + dbLocation + BD_NAME);    
            fileConnection.truncate(0);
            fileConnection.openOutputStream();
            // Si el archivo está en blanco se copia la BD pre-definida a la tarjeta SD
            if(fileConnection.exists() && fileConnection.fileSize() == 0)
            {                      
                readAndWriteDatabaseFile(fileConnection);           
            }         
            
            fileConnection.close();
            // Se abre la base de datos           
            db = DatabaseFactory.open(uri);

In my case the database file is bundled with my application i don't download it over the network.

 


 I get the error at readAndWriteDatabaseFile(fileConnection); it reads File System Error (1005)

 

Please use plain text.
New Developer
akash1212
Posts: 16
Registered: ‎12-18-2008
My Device: Not Specified

Re: Problem opening a database from SDCard that has been encrypted

Hi All,

 

I have a similar problem but in my case I am downloading a media file (.3gp) using inputstream and then writing into SD card using file.outputstream. 

 

The issue is, when the entire file is downloaded, the file is completely encrypted. All the bytes have been changed and my application can not play the file.

 

The steps followed are,

 

1. Create a file (eg. 123.3gp)

2. Open Output stream with the file. (fconn.openOutputStream())

3. Download a part of file (httpConn.InputStream)

4. Write this chunk into file created (os.write)

5. Follow step 3 & 4 untill the entire file is downloaded.

 

Now if I check the file, all the bytes have changed. (Without encryption the code works perfectly fine)

 

Please let me know if any one can help .

 

Thanks,

Akash.

 

Please use plain text.