私は SQLite データベースを使用してアプリケーションを作成しています。SQLite データベースをバックアップするためのコードを既に作成しました。このようなコピーからアプリケーション データベースを復元できるようにしたいと考えています。Android デバイスの [開く] ダイアログを使用しています。リスト内の他のコンテンツ プロバイダー (たとえば [Bluetooth ファイル転送]) を使用すると、ファイルが表示されます。ただし、[ダウンロード] オプションを使用すると、ファイルが表示されません。
ダウンロードフォルダにSQLiteデータベースをコピーしました。fileIntent.setType("を使用しようとしました/()。
ありがとう。
ベストアンサー1
それは ですapplication/x-sqlite3
。私はこれを自分のアプリで使用しています。
これは例どのように使用するか:
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クラス:
package com.example.myapp;
import android.support.v4.content.FileProvider;
public class MyFileProvider extends FileProvider {
}
そして、それを宣言する方法は次のとおりですマニフェスト:
<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
現在は廃止されているようです。ファビアンの答え詳細については、以下をご覧ください。