rokkonet

PC・Androidソフトウェア・アプリの開発・使い方に関するメモ

android開発 Intentでのファイラー起動

2023 Feb. 11.
2023 Jan. 15.

環境

Android 11
compileSdk 33
minSdk 30
targetSdk 33


ファイラーを起動できるIntent

Intent.ACTION_GET_CONTENT
Intent.ACTION_OPEN_DOCUMENT
Intent.ACTION_OPEN_DOCUMENT_TREE

"Intent.ACTION_OPEN_DOCUMENT"、"Intent.ACTION_GET_CONTENT"ではIntent#setType()を指定しないといけない。すべてのファイルならsetType("*/*")。
Storage Access Frameworkの制約を考慮すること。
ファイルストレージの読み込み権限を取得しておくこと。

Intentリスト

Intent  |  Android Developers

サンプルコード

ACTION_GET_CONTENTモードでデフォルトファイラーが起動
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.setType("*/*")
try {
    startActivity(intent)
} catch (e: ActivityNotFoundException) {
    // Define what to be done if no activity can handle the intent.
}


ACTION_OPEN_DOCUMENTモードでデフォルトファイラーが起動
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.setType("*/*")
try {
    startActivity(intent)
} catch (e: ActivityNotFoundException) {
    // Define what to be done if no activity can handle the intent.
}


ACTION_OPEN_DOCUMENT_TREEモードでデフォルトファイラーが起動
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
    flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
}
try {
    startActivity(intent)
} catch (e: ActivityNotFoundException) {
    // Define what to be done if no activity can handle the intent.
}


ACTION_GET_CONTENTモードでX-ploreが起動
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.setPackage("com.lonelycatgames.Xplore")
intent.setType("*/*")
try {
    startActivity(intent)
} catch (e: ActivityNotFoundException) {
    // Define what to be done if no activity can handle the intent.
}


ACTION_GET_CONTENTモードで複数ファイラーアプリ選択画面を期待するも、デフォルトファイラーが起動する
val title = resources.getString(R.string.chooser_title)
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.setType("*/*")
val chooser = Intent.createChooser(intent, title)
try {
    startActivity(chooser)
} catch (e: ActivityNotFoundException) {
    // Define what your app should do if no activity can handle the intent.
}


ACTION_GET_CONTENTモードでX-ploreが起動する
val title = resources.getString(R.string.chooser_title)
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.setType("*/*")
intent.setPackage("com.lonelycatgames.Xplore")
val chooser = Intent.createChooser(intent, title)
try {
    startActivity(chooser)
} catch (e: ActivityNotFoundException) {
    // Define what your app should do if no activity can handle the intent.
}


ACTION_OPEN_DOCUMENT_TREEモードでX-plore起動を期待するも、何も起動しない
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
    flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
}
intent.setPackage("com.lonelycatgames.Xplore")
try {
    startActivity(intent)
} catch (e: ActivityNotFoundException) {
    // Define what your app should do if no activity can handle the intent.
}