rokkonet

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

android開発 ContentResolver 複数のメディアタイプについてURI・ファイルパスを取得する

2022 May 04.

端末

android 11 ( APIレベル 30 )

audioメディアとvideoメディアのURI・ファイルパスを取得するサンプルアプリ

MainActivity.kt
package net.sytes.rokkosan.mygetaudioandvideouriwithcontentresolver

/*
2022 May 04.
2022 May 03.
Ryuichi Hashimoto.
*/

import android.content.ContentUris
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {
    lateinit var textViewDebugInfo: TextView

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

        textViewDebugInfo = findViewById(R.id.textViewDebugInfo)

        findViewById<Button>(R.id.buttonGetUris).setOnClickListener {
            getPathsOfUris().forEach { (key, value) ->
                Log.d("MyTag", key + " : " + value.toString())}
        }
    }

    private fun getPathsOfUris(): HashMap<String, Uri> {
        val uriMap = hashMapOf<String, Uri>()

        // referrence
        //     https://qiita.com/TaigaNatto/items/239c87080ac766c97ac9
        val contentResolver = this.contentResolver
        val proj = arrayOf(
            MediaStore.MediaColumns._ID,
            MediaStore.Files.FileColumns.MEDIA_TYPE,
            "_data"
        )

        val selection = (MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                + MediaStore.Files.FileColumns.MEDIA_TYPE_AUDIO
                + " OR "
                + MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO
                )

        val query = contentResolver.query(
            MediaStore.Files.getContentUri("external"),
            proj,
            selection,
            null,
            MediaStore.Files.FileColumns.DATE_ADDED + " DESC"
        )
        query?.use { cursor ->
            val idColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID)
            val typeColumn = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MEDIA_TYPE)
            val pathColumn = cursor.getColumnIndexOrThrow("_data")

            while (cursor.moveToNext()) {
                val mediaType = cursor.getInt(typeColumn)
                // mediaType  0:none 1:image 2:audio 3:video 4:playlist 5:subtitle 6:document
                // referrence
                //     https://developer.android.com/reference/android/provider/MediaStore.Files.FileColumns#MEDIA_TYPE

                // audioとvideoのURIを取得
                var mediaId: Long
                var mediaUri: Uri
                var pathOfUri: String
                if (mediaType == 2) { // audio
                    mediaId = cursor.getLong(idColumn)
                    mediaUri = ContentUris.withAppendedId(
                        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, mediaId
                    )
                    pathOfUri = cursor.getString(pathColumn)
                    uriMap.put(pathOfUri, mediaUri)
                }
                else if (mediaType == 3) { // video
                    mediaId = cursor.getLong(idColumn)
                    mediaUri = ContentUris.withAppendedId(
                        MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaId
                    )
                    pathOfUri = cursor.getString(pathColumn)
                    uriMap.put(pathOfUri, mediaUri)
                }
            }
        }
        query?.close()
        return uriMap
    }
}


AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.sytes.rokkosan.mygetaudioandvideouriwithcontentresolver">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyGetAudioAndVideoUriWithContentResolver">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


build.gradle(Module: app)
plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "net.sytes.rokkosan.mygetaudioandvideouriwithcontentresolver"
        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/textViewDebugInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/buttonGetUris"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/buttonGetUris"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_label"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textViewDebugInfo" />

</androidx.constraintlayout.widget.ConstraintLayout>


strings.xml
<resources>
    <string name="app_name">MyGetAudioAndVideoUriWithContentResolver</string>
    <string name="button_label">Get URIs</string>
</resources>