I'm writing an application using SQLite database. I already coded for backing up my SQLite database. I now want to be able to restore my application database from such a copy. I am using Android device "Open from" dialog. I see the file if I use other content providers in the list, such as for example "Bluetooth File Transfer"! But I don't see it if I try to use the "Downloads" option.
I copied a SQLite database in my downloads folder. I tried to use fileIntent.setType("/").
Thanks.
It's application/x-sqlite3
. I use this in my own app.
More info here.
Here's an example of how I use it:
File backupDB = ...;
//Uri uri = Uri.fromFile(backupDB); //From Android 7, this line results in a FileUriExposedException. Therefore, we must use MyFileProvider instead...
Uri uri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".com.example.myapp.myfileprovider", backupDB);
Intent newIntent = new Intent(Intent.ACTION_VIEW);
newIntent.setDataAndType(uri, "application/x-sqlite3");
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(newIntent);
For completeness, here is my MyFileProvider.java class:
package com.example.myapp;
import android.support.v4.content.FileProvider;
public class MyFileProvider extends FileProvider {
}
And here's how to declare it in the manifest:
<provider
android:name=".MyFileProvider"
android:authorities="${applicationId}.com.example.myapp.myfileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/my_file_provider_paths"/>
</provider>
And, finally, here is my my_file_provider_paths.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="."/>
</paths>
### Update (20th Nov 2019) ###
Apparently, application/x-sqlite3
is now deprecated. Please see Fabian's answer, below, for more information.
Since Feb. 2018 the SQLite3 format has an official media type registered at IANA: application/vnd.sqlite3
. The usage of application/x-sqlite3
is deprecated and should only be used if backwards-compatibility is required.
See:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With