Ad

In Which Directory Is Being Stored A File When I Use "Activity.openFileOutput("myFile", Context.MODE_PRIVATE);" To Store It?

I have this code to store a remote sqlite .db file with my android app:

public String getRemoteCollection(String url, String dbName){
    BufferedInputStream in = null;
    FileOutputStream fout = null;
    dbName="myFile";
    try
    {
        in = new BufferedInputStream(new URL(url).openStream());
        fout = SectionManager.instance.activity.openFileOutput(dbName, Context.MODE_PRIVATE);

        byte data[] = new byte[1024];
        int count;
        while ((count = in.read(data, 0, 1024)) != -1)
            fout.write(data, 0, count);
        if (in != null)
            in.close();
        if (fout != null)
            fout.close();                   
    }
    catch(Exception e){e.printStackTrace();}
    return dbName;
}

I need to open that file with my sqlitemanager, and I have this function, that opens a .db file from assets, but I need to adapt this method to load the file I stored in the previous mentioned method, which is not stored on assets folder. So I must change this method but I don't know how to do it correctly.

private void generateSQLiteDB(String databaseName) {     // 
        SQLiteDatabase db =  dbHelper.getReadableDatabase(); // by calling this line an empty database will be created into the default system path of this app - we will then overwrite this with the database from the server
        db.close();
        OutputStream os = null;
        InputStream is = null;
        try{
            is =  ctx.getAssets().open(databaseName+".db");
            String packageName=SectionManager.instance.activity.getApplicationContext().getPackageName();
            //os = new FileOutputStream("/data/data/com.android.launcher/databases/"+databaseName+".db");   
            os = new FileOutputStream("/data/data/"+packageName+"/databases/"+databaseName+".db");  
            copyFile(os, is);
        }catch (Exception e) {
            Log.e("DB", "Database not found", e);                          
        }finally{
            try{
                //Close the streams
                if(os != null)
                    os.close();
                if(is != null)
                    is.close();
            } catch (IOException e) {Log.e("DB", "Can't close adapters");}
        }
    }
Ad

Answer

openFileOutput(), as the documentation states, does not take a path, the file is stored as part of your app's 'private data storage', which is inaccessible to other apps on the device. So assuming your two code snippets above are in the same Android app, you don't need to worry about the path, just pass the same file name to openFileInput() and you'll get the same data that you stored.

In your case, your file name is dbName, so in your second snippet:

is =  ctx.getAssets().open(databaseName+".db");

becomes

is = ctx.openFileInput(databaseName);

which appears to be hard-coded to 'myFile' in the first snippet. I am assuming here that ctx is of type Context or a derived class such as Activity.

UPDATE: If you really need to find out the file system directory being used to store your apps private files, you can call:

ctx.getFilesDir();

or

ctx.getFileStreamPath(databaseName);
Ad
source: stackoverflow.com
Ad