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

生来就可爱ヽ(ⅴ<●) 提交于 2020-12-02 07:04:48

问题


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.


回答1:


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.




回答2:


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



回答3:


I don't have a complete answer. Instead I'll offer a few ideas.

I'm not sure what you mean by the "Open from" dialog. I'm assuming you mean the "Chooser" dialog, which shows the icons of apps which can handle the intent.

Many Android apps use MimeTypeMap for associating file extensions and MIME types. There is no method to iterate over MimeTypeMap to see what all of its mappings are. I'm fairly confident these are the mappings. There is no entry in the list for SQLite. A conventional MIME type for SQLite would be "application/x-sqlite3".

In your post, you included fileIntent.setType("/"). I'm guessing you actually typed fileIntent.setType("*/*"), and because it was not quoted the formatter ate the two stars.

Because you can give any extension you want to your SQLite database files, you have some flexibility in choosing one that produces the results you want. I'm guessing you want to find something that will not only cause Download to be included in the chooser dialog, but also exclude unwanted apps. You'll probably have to experiment after looking at the list of mappings supported by MimeTypeMap.



来源:https://stackoverflow.com/questions/31301552/android-what-is-the-mime-type-to-use-if-i-want-to-see-pick-a-sqlite-database-fr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!