1

I am an abject novice in Android app development. However, my current project requires me to make an app that records field observations (am a research scientist), save them to a SQLite database - these will be sent to a central location for processing. The app will be used on tablets.

Currently, I am using Android Studio, and when I emulate the app functions, everything works as planned and using DB Browser for SQLite, I can see that the values are being recorded correctly in the database.

Doing research (an example is the answer here), I am aware that the the database (.db) file is saved in the directory path:

/data/data/<your.app.package>/databases/foo.db

Of course, this is accessible in Android Studio, but my understanding is that unless the device is rooted, these files are inaccessible in an actual device. For various reasons, this is not an option for the work that I am involved with.

In researching, I came across a method to back up the database, and save the back up database to an SD card, as in the answer to the question Backup/restore sqlite db in android [duplicate] and the duplicate target Android backup/restore: how to backup an internal database?.

Saving to external storage devices such as SD cards in APIs of 23 and above now require considerable more permissions to write to external sources (above and beyond what has always been required in the Manifest). For many reasons, this option is not optimal.

In this answer, the backup is saved on the SD card:

final String inFileName = "/data/data/<your.app.package>/databases/foo.db";
File dbFile = new File(inFileName);
FileInputStream fis = new FileInputStream(dbFile);

String outFileName = Environment.getExternalStorageDirectory()+"/database_copy.db";

// Open the empty db as the output stream
OutputStream output = new FileOutputStream(outFileName);

// Transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer))>0){
    output.write(buffer, 0, length);
}

// Close the streams
output.flush();
output.close();
fis.close();

But, my question (possibly a brain-fart type) is - is it possible to have the backup saved to an internal file of the device (e.g. Documents folder), and would the permissions still be required?

Community
  • 1
  • 1
  • what do you mean by internal file of device? – H Raval Oct 18 '16 at 09:49
  • @HRaval saved in accessible area on the device itself (hence my example, say the 'documents' folder, or equivalent). –  Oct 18 '16 at 09:51
  • You can't store it in a "public enviroment". However, you can backup in an internal app folder (this stil no accessible for the user). "these will be sent to a central location for processing" do you mean manually ? – crgarridos Oct 18 '16 at 10:02
  • @cgarrido at this stage, yes. –  Oct 18 '16 at 10:05
  • after 6.0 you have to manage user permissions...there is no shortcut...and to backup within app...not make any seance...isn't it? – H Raval Oct 18 '16 at 10:06
  • It was make sense in that it would make the db file easier to access. –  Oct 18 '16 at 10:15
  • @DamienPaul please check my answer, it's not hard to implement – crgarridos Oct 18 '16 at 13:44
  • Make db available on any device. Backup to cloud. There is probably a free solution available somewhere. – danny117 Oct 18 '16 at 15:34

1 Answers1

1

It's not complicated to achieve the pemission's flow. Here is a class that I created for help. You have to extend your MainActivity from this, and then you can call:

verifyStoragePermissions(new Runnable() {
    @Override
    public void run() {
        //backup your db here...
    }
});

It works for all Android versions using android support library.

crgarridos
  • 8,758
  • 3
  • 49
  • 61