1

I am implementing a solution that saves multiple versions of SQLite database file. Each time the db is saved it is saved with a different file name, I append datetime to make the difference. This works fine and I am able to save multiple version of the SQLite database with different time stamps like this:

/storage/emulated/0/AppName/Backups/auto_backup_2016_Feb_26_07_09 

Now what I want is to save this path to database or SharedPreference and when needed I want to be able to restore. To be able to restore the file, first I need to get access again to the file, so I am trying to either get access or recreate the file again using the saved path to that file.

I tried using getAbsolutePath and that did not work and now I am trying Uri. Here is how I save the file

File backupDatabaseFile = new File(backupFolder, Constants.AUTO_BACKUP + "_" + TimeUtils.getDatetimeSuffix(System.currentTimeMillis())); Uri uri = Uri.fromFile(backupDatabaseFile); backupItem.setFilePath(uri.toString()); 

And here is how I attempt to recreate the saved file so I can restore it:

Uri uri = Uri.parse(backupItem.getFilePath); File backedUpFile = new File(uri.getPath()); 

And here is where it fails

 if (backedUpFile.exists()) { //do something } 

So how can I can save the path to a file, and then use this path to re-create or access the same file later.

1
  • Can you add the exception raised upon failure? It's weird that it fails on the exists method. The only thing that could explain it is that backedUpFile is null which means that the new File wasn't executed Commented Feb 26, 2016 at 8:14

3 Answers 3

2

new File(String) doesn't create physical file by itself. Just call

 File f = new File(path); if(!f.exists()){ f.createNewFile(); } 
Sign up to request clarification or add additional context in comments.

Comments

1

I think your backupDatabaseFile is empty. Did you create your backupDatabaseFile as follows?

 backupDatabaseFile=new File("/storage/emulated/0/AppName/Backups/auto_backup_2016_Feb_26_07_09"); 

5 Comments

Yes, I created it like File backupDatabaseFile = new File(backupFolder, Constants.AUTO_BACKUP + "_" + TimeUtils.getDatetimeSuffix(System.currentTimeMillis()));
And I can see that I have multiple files created
Did you try File backedUpFile = new File(backupItem.getFilePath)); Incase you did it is still showing null?
I tried and it created a file like this file:///storage/emulated/0/AppName/Backups/auto_backup_2016_Feb_26_07_39
How are you creating backup files? The file path outputFileName = getFilesDir().getPath() + File.separator + System.currentTimeMillis(); works for me. But this issue does seem strange.
0

Turns out the issue was that I was trying to create a File in a class that does not have Context. I move the following statement to a class that has Context and it worked right away

public static final String BACKUP_FOLDER = "AppName/Backups"; File backupFolder = new File(Environment.getExternalStoragePublicDirectory(Constants.BACKUP_FOLDER), ""); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.