I am trying to do the same thing but with a database file.  And I can
successfully copy a text file to the data directory.

But when I use a sqlite3 db file (either one I pulled off android
emulator data directory or one I made on my desktop machine), I throw
an exception.

Every time I run the read method on a the steam from the db file, I
get an java.io.IOException with no details.  Why is this?

I've done some reading and I understand that binary files and unicode
files must be read differently (1 byte versus 2 bites for unicode),
but I thought that readers were for unicode and inputstreams were for
binary.  Still, I tried using readers and that didn't work either.

The following code works perfectly to copy a file named textfile.txt
from my raw resource folder.  However, when I switch the text file for
an actual myapp.db file (even if I try using the same name as the text
file), I get an exception.  If I comment out the while loop, the error
goes away.  And it's the read that has the problem.  If I comment out
just the write line, I still throw an excpetion.

Is there something different about this file type that won't allow me
to read a stream from it as I am doing?

private void copyDataBase() throws IOException{

        //Path to the just created empty db, which I carefully closed so I
have safe write access
        OutputStream databaseOutputStream = new FileOutputStream("/data/
data/com.mydomain.myapp/databases/myapp.db");
        InputStream databaseInputStream =
databaseOpenHelperContext.getResources().openRawResource
(R.raw.textfile);

        byte[] buffer = new byte[1];
        int length;
        while ( (length = databaseInputStream.read(buffer)) > 0 ) {
                databaseOutputStream.write(buffer);
                Log.w("Bytes: ", ((Integer)length).toString());
                Log.w("value", buffer.toString());
        }

        //Close the streams
        databaseOutputStream.flush();
        databaseOutputStream.close();
        databaseInputStream.close();
 }

On Feb 11, 12:49 pm, Mark Murphy <[email protected]> wrote:
> Anders wrote:
> > I have a simple problem. I want to put a file into my applications
> >apk, and upon installation I would like tocopythis file into the
> >devicefilesystem. Does anyone know how to do this?
>
> Package the file as a raw resource. Get an input stream for the
> resource. Get an output stream for your destination file. From there,
> it's a standard Java I/Ocopyoperation:
>
> http://www.exampledepot.com/egs/java.io/CopyFile.html
>
> --
> Mark Murphy (a Commons Guy)http://commonsware.com
>
> Android Training on the Ranch! -- Mar 16-20, 
> 2009http://www.bignerdranch.com/schedule.shtml

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to