I'm trying to update an old Android application that uses absolute paths for files. Instead, the plan is to allow users to select a file. This file, an SQLite database, will then be moved to the application's data directory. The purpose is to allow users to restore backed-up versions of the app's data, overwriting what's currently there. However, after opening the file I get a URI along the lines of /document/primary:app_name.db
, which I'm not able to open. Here's some code (target SDK version set to 28):
public void importDb()
{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//intent.setType("application/vnd.sqlite3"); // Doesn't allow selecting the file
intent.setType("*/*");
startActivityForResult(intent, READ_REQUEST_CODE);
}
// Handle file selection here.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == READ_REQUEST_CODE && resultCode == RESULT_OK && data != null) {
Uri uri = data.getData();
replaceDatabase(uri);
}
}
public void replaceDatabase(Uri uri)
{
infile = new File(uri.getPath());
if(!(infile.exists()))
{
Log.e(TAG,"Infile does not exist: " + infile); // This log error is seen, so the URI wasn't right.
return;
}
....
copy(infile, outfile);
....
}
Is anyone able to suggest why this is failing at the infile.exists()
point? Presumably that Uri isn't a valid path, and if so I'd appreciate any suggestions on how to obtain one.