
【Android,Kotlin】GetContentで端末に保存された画像ファイルを選択して表示する
アプリのイメージ 端末に保存されている画像を選択したい。
関連: 【Android,Kotlin】OpenDocumentで端末内の画像ファイルを選択して表示する
実装 レイアウト ImageViewとボタンを設置しておく。
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="300dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/next" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/imageView" /> </androidx.constraintlayout.widget.ConstraintLayout> Fragment/ViewModel フィールドにregisterForActivityResultでActivityResultContracts.GetContent()のlauncherを作る。