rokkonet

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

android開発 外部ストレージ共有領域の特定ディレクトリ内のメディアファイル取得

2022 May 08.
2022 Apr. 30.

端末

android 11 ( APIレベル 30 )

参考

android開発 ContentResolver 音声メディア・動画メディアへのクエリによるメディア情報取得 - rokkonet

概要

ContentResolverのprojectionに"_data"をセットしてメディアファイルをクエリーし、"_data"から取得したディレクトリで取捨選択する。
("_data"にファイルパスが入っている)

外部ストレージ共有領域内のディレクトリの取得
android開発 deprecatedなメソッドを使わずに外部ストレージ共有領域のパスを取得するにはファイルピッカーを使うしかないと思われる - rokkonet

サンプルkotlinコード
        val collection =
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                MediaStore.Audio.Media.getContentUri(
                    MediaStore.VOLUME_EXTERNAL
                )
            } else {
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
            }

        val projection = arrayOf(
            MediaStore.Audio.Media._ID,
            "_data"
        )

        // 1分間以上のAudioを指定する
        val selection = "${MediaStore.Audio.Media.DURATION} >= ?"
        val selectionArgs = arrayOf(
            TimeUnit.MILLISECONDS.convert(1, TimeUnit.MINUTES).toString()
        )

        val resolver = applicationContext.contentResolver
        val query = resolver.query(
            collection,  //データの種類
            projection, //取得する項目 nullは全部
            selection, //フィルター条件 nullはフィルタリング無し
            selectionArgs, //フィルター用のパラメータ
            null   //並べ替え。nullは並べ替えしない
        )

        query?.use { cursor ->
            val pathColumn = cursor.getColumnIndexOrThrow("_data")

            while (cursor.moveToNext()) {
                val pathOfUri = cursor.getString(pathColumn)
                // ここで、pathOfUriの文字列を調べてメディアファイルを取捨選択する
            }
        }
        query?.close()


android開発 deprecatedなメソッドを使わずに外部ストレージ共有領域のパスを取得するにはファイルピッカーを使うしかないと思われる

2022 May 01.
2022 Apr. 30.

Android 11 (API レベル 30)にて
deprecatedなメソッドを使わずに外部ストレージ共有領域のパスを取得するにはファイルピッカーを使うしかないと思われる。

参照
android開発 ファイル・ピッカーを開いてディレクトリを選択するには Intent.ACTION_OPEN_DOCUMENT_TREE を利用する - rokkonet
android開発 Intent.ACTION_OPEN_DOCUMENT_TREEでファイル・ピッカーから選択した外部ストレージ内ディレクトリのURIのAuthorityは、すべて "com.android.externalstorage.documents" となっている - rokkonet

android開発 ダイアログから呼び出し元に値を渡す

2022 Apr. 29.

(1) ダイアログ内で、呼び出し元のContextを通じて、呼び出し元に値を渡す
  android開発 ダイアログから呼び出し元Activityに値を渡す(Contextを利用) - rokkonet

(2) 独自リスナーを定義して呼び出し元に値を渡す
  android開発 ダイアログから呼び出し元に値を渡す(独自リスナー利用) - rokkonet

android開発 Fragment内でnon-nullなActivity Contextを取得する関数はrequireContext()

2022 Apr. 29.
2022 Apr. 28.

出典
日付選択ダイアログを追加する記述の、contextのType mismatch。
Android で一般的な Kotlin パターンを使用する  |  Android デベロッパー  |  Android Developers

Fragment#requireContext : non-nullなActivity Contextを返す
Fragment#getContext : nullableなActivity Contextを返す

サンプルkotlinコード

class CustomDialogFragment : DialogFragment() {
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val myDialog = Dialog(requireContext())
        myDialog.getWindow()?.requestFeature(Window.FEATURE_NO_TITLE)
        myDialog.setContentView(R.layout.my_dialog)
        val editTextMessage = myDialog.findViewById<EditText>(R.id.editTextMessage)
        myDialog.findViewById<Button>(R.id.buttonOk).setOnClickListener {
                val text = editTextMessage.text.toString()
                val callingActivity = activity as MainActivity
                callingActivity.onReturnValue(text)
                dismiss()
        }
        return myDialog
    }
}

android開発 ダイアログから呼び出し元Activityに値を渡す(Contextを利用)

2022 Apr. 29.
2022 Apr. 24.

出典 DialogFragment を利用したカスタムダイアログからActivityに値を返す | iPentec
参考 android開発 ダイアログから呼び出し元に値を渡す - rokkonet

概要

  • 呼び出し元Activityに、値を取得するメソッドonReturnValue()を定義する
  • ダイアログ内で呼び出し元のコンテクストを取得し、変数callingActivityに保持する
    (Dialogクラスのコンストラクタの引数に呼び出し元のコンテクストがセットされている)
  • ダイアログのOKボタンのonClickイベントで、callingActivity.onReturnValue()を実行する

    build.gradle(Module: app)

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "net.sytes.rokkosan.mypassvaluefromdialogtoactivity"
        minSdk 30
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textViewInfo1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toTopOf="@id/buttonOpenDialog"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/buttonOpenDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_activity_label"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textViewInfo1" />

</androidx.constraintlayout.widget.ConstraintLayout>


my_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textViewGuide"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text_view_guide"
        app:layout_constraintBottom_toTopOf="@id/editTextMessage"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonOk"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editTextMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toTopOf="@id/buttonOk"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textViewGuide" />

    <Button
        android:id="@+id/buttonOk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_dialog_label"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/editTextMessage" />
</androidx.constraintlayout.widget.ConstraintLayout>


MainActivity.kt

package net.sytes.rokkosan.mypassvaluefromdialogtoactivity

/*
2022 Apr. 24.
Ryuichi Hashimoto.
This is a copy of the codes in https://www.ipentec.com/document/android-custom-dialog-using-dialogfragment-return-value
*/

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val buttonOpenDialog = findViewById<Button>(R.id.buttonOpenDialog)
        buttonOpenDialog.setOnClickListener { showMyDialog() }
    }

    private fun showMyDialog() {
        val cdf = CustomDialogFragment()
        cdf.show(supportFragmentManager, "dialog")
    }

    public fun onReturnValue(value: String) {
        val textViewInfo1 = findViewById<TextView>(R.id.textViewInfo1)
        textViewInfo1.text = value
    }
}


CustomDialogFragment.kt

package net.sytes.rokkosan.mypassvaluefromdialogtoactivity

/*
2022 Apr. 28.
2022 Apr. 24.
Ryuichi Hashimoto.
This is a copy of the codes in https://www.ipentec.com/document/android-custom-dialog-using-dialogfragment-return-value
*/

import android.app.Dialog
import android.os.Bundle
import android.view.Window
import android.widget.Button
import android.widget.EditText
import androidx.fragment.app.DialogFragment

class CustomDialogFragment : DialogFragment() {
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val myDialog = Dialog(requireContext())
        myDialog.getWindow()?.requestFeature(Window.FEATURE_NO_TITLE)
        myDialog.setContentView(R.layout.my_dialog)
        val editTextMessage = myDialog.findViewById<EditText>(R.id.editTextMessage)
        myDialog.findViewById<Button>(R.id.buttonOk).setOnClickListener {
                val text = editTextMessage.text.toString()
                val callingActivity = activity as MainActivity
                callingActivity.onReturnValue(text)
                dismiss()
        }
        return myDialog
    }
}

android開発 Intent.ACTION_OPEN_DOCUMENT_TREEでファイル・ピッカーから選択した外部ストレージ内ディレクトリのURIのAuthorityは、すべて "com.android.externalstorage.documents" となっている

2022 May 01.
2022 Apr. 30.
2022 Apr. 10.

利用android端末のバージョン 11

build.gradle(:app)

    compileSdk 32

    defaultConfig {
        minSdk 24
        targetSdk 32
    }


手元のAndroid端末で、Intent.ACTION_OPEN_DOCUMENT_TREEでファイル・ピッカーを開き、選択した外部ストレージ内ディレクトリのURIのAuthorityを調べると、すべて "com.android.externalstorage.documents" となっている。

URI全体は、"content://com.android.externalstorage.documents/tree/STORAGEDIR%3AFIRSTDIR%2FSECONDDIR%2FTHIRDDIR"(%3Aはコロン、%2Fはスラッシュのエンコード文字)となっている。
URI全体は、次のように文字列を置き換えると読みやすい。

myUri.toString().replace(Regex("^.*tree/"),"/").replace("%3A","/").replace("%2F","/")


Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
    addFlags(
        Intent.FLAG_GRANT_READ_URI_PERMISSION or
        Intent.FLAG_GRANT_WRITE_URI_PERMISSION or
        Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION or
        Intent.FLAG_GRANT_PREFIX_URI_PERMISSION
    )
}

android開発 ストレージ内のファイルの種別(URIのauthorityの種別)

2022 Apr. 29.
2022 Apr. 09.

出典
Android: Files: Unable to get file path from content URI when file is selected from RECENT section in file browser - Stack Overflow
android - cursor didn't have _data column not found - Stack Overflow
Androidで 静止画と動画のピッカーで選択したコンテンツのファイルパスを取得する - 酢ろぐ!

各種サイトの情報からすると、URIのauthorityに4つの種別がある?

"com.android.externalstorage.documents"
"com.android.providers.downloads.documents"
"com.android.providers.media.documents"
"com.google.android.apps.photos.content"

fun isExternalStorageDocument(uri: Uri): Boolean {
        return "com.android.externalstorage.documents" == uri.authority
}

fun isDownloadsDocument(uri: Uri): Boolean {
        return "com.android.providers.downloads.documents" == uri.authority
}

fun isMediaDocument(uri: Uri): Boolean {
        return "com.android.providers.media.documents" == uri.authority
}

fun isGooglePhotosUri(uri: Uri): Boolean {
        return "com.google.android.apps.photos.content" == uri.authority
}