Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: what is the mime type to use if I want to see/pick a SQLite database from the Downloads folder?

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.

like image 958
Serge Plourde Avatar asked Jul 08 '15 19:07

Serge Plourde


2 Answers

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.

like image 154
ban-geoengineering Avatar answered Oct 16 '22 08:10

ban-geoengineering


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:

  • http://sqlite.1065341.n5.nabble.com/Re-Why-is-Sqlite-mediatype-not-registered-at-iana-td97826.html
  • https://www.iana.org/assignments/media-types/application/vnd.sqlite3
like image 27
Fabian Avatar answered Oct 16 '22 08:10

Fabian