AndroidManifest.xmlを編集
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="jp.digitaldolphins.mydd.fileprovider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
android.support.v4.content.FileProviderを宣言します。
res/xml/file_paths.xmlを作成
<paths xmlns:android="http://schemas.android.com/apk/res/android" >
<files-path
name="my_files"
path="my_files/" />
</paths>
一時ファイルを作成
File dir = new File(MobileSearchActivity.this.getFilesDir(), "my_files"); dir.mkdir(); File fp2 = new File(dir, title); FileOutputStream os = new FileOutputStream(fp2);フォルダ階層有りのファイルを作るときは、FileOutputStreamで良いようです。Context.openFileOutputを使うことばかり考えていましたが、そうではないみたいです。
インテントを作動させて、他のアプリを開く
Uri uriForFile = FileProvider.getUriForFile(
MobileSearchActivity.this,
"jp.digitaldolphins.mydd.fileprovider", fp2);
Intent nIntent = new Intent(Intent.ACTION_VIEW);
nIntent.setDataAndType(uriForFile, mime);
nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
List<ResolveInfo> resInfoList = getPackageManager()
.queryIntentActivities(nIntent,
PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
grantUriPermission(resolveInfo.activityInfo.packageName,
uriForFile, Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
MobileSearchActivity.this.startActivity(nIntent);
jp.digitaldolphins.mydd.fileproviderとfp2とmimeとMobileSearchActivity.thisなどは、自分の物で書き換えます。後はコピペで行けると思います。
繰り返しが入っているのは、他のアプリへのアクセスを許可する為のコードです。
API level 9 準拠
0 件のコメント:
コメントを投稿