Android:如果我想从Downloads文件夹中查看/选择SQLite数据库,可以使用哪种mime类型? [英] Android: what is the mime type to use if I want to see/pick a SQLite database from the Downloads folder?

查看:70
本文介绍了Android:如果我想从Downloads文件夹中查看/选择SQLite数据库,可以使用哪种mime类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SQLite数据库编写应用程序.我已经编写了备份SQLite数据库的代码.现在,我希望能够从这样的副本中还原我的应用程序数据库.我正在使用Android设备的从...打开"对话框.如果我在列表中使用其他内容提供程序,例如蓝牙文件传输",我会看到该文件!但是,如果我尝试使用下载"选项,则看不到它.

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.

我在下载文件夹中复制了一个SQLite数据库.我尝试使用fileIntent.setType("/").

I copied a SQLite database in my downloads folder. I tried to use fileIntent.setType("/").

谢谢.

推荐答案

它是 application/x-sqlite3 .我在自己的应用中使用它.

It's application/x-sqlite3. I use this in my own app.

此处有更多信息.

这是我的用法示例:

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);

为完整起见,这是我的 MyFileProvider.java 类:

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>

最后,这是我的 my_file_provider_paths.xml :

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="external_files" path="."/>
</paths>

###更新(2019年11月20日)###

显然,现在已弃用了 application/x-sqlite3 .有关更多信息,请参见下面的 Fabian的答案.

Apparently, application/x-sqlite3 is now deprecated. Please see Fabian's answer, below, for more information.

这篇关于Android:如果我想从Downloads文件夹中查看/选择SQLite数据库,可以使用哪种mime类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆